Skip to content

Instantly share code, notes, and snippets.

@zard1989
Created January 26, 2010 18:05
Show Gist options
  • Select an option

  • Save zard1989/287053 to your computer and use it in GitHub Desktop.

Select an option

Save zard1989/287053 to your computer and use it in GitHub Desktop.
asInt :: String -> Int
asInt xs = loop 0 xs
loop acc [] = acc
loop acc (x:xs) = let acc' = acc * 10 + digitToInt x
in loop acc' xs
loopF :: Double -> String -> Double
loopF acc [] = acc
loopF acc (x:xs) = let acc' = (acc + fromIntegral (digitToInt x))*0.1
in loopF acc' xs
asDecimal :: String -> Double
asDecimal xs = loopF 0 (reverse xs)
asDouble :: String -> Double
asDouble xs =
if null (filter (=='.') xs)
then fromIntegral $ asInt xs
else let (intPart, '.':decPart) = break (=='.') xs
in (fromIntegral $ asInt intPart) + (asDecimal decPart)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment