Converter Fahrenheit <--> Celsius. Written in Haskell.




  
main :: IO()
main = do
    putStr("\nEnter 'f' to convert from Fahrenheit or 'c' to convert from Celsius: \n\n")
    fromScale <- getLine
    if fromScale == "f"
        then do
                print("Enter Fahrenheit temperature: ")
        else do
                print("Enter Celsius temperature: ")
    line <- getLine
    let floatInputTemp = stringToFloat line

    if fromScale == "f"
        then do
                let result = f_to_c floatInputTemp
                let rounded = roundNumber 2 result
                putStrLn $ "Celsius temperature " ++ show rounded
        else do
                let result = c_to_f floatInputTemp
                let rounded = roundNumber 2 result
                putStrLn $ "Fahrenheit temperature: " ++ show rounded

stringToFloat :: String -> Float
stringToFloat s = read s

f_to_c :: Float -> Float
f_to_c a = 5/9 * ( a - 32 )

c_to_f :: Float -> Float
c_to_f a = ( a * 9/5 ) + 32

roundNumber :: Int -> Float -> Float
roundNumber n x = fromIntegral (truncate $ x * 10^n) / 10^n