haskell - How to convert a Rational into a "pretty" String? -


i want display rational values in decimal expansion. is, instead of displaying 3 % 4, rather display 0.75. i'd function of type int -> rational -> string. first int specify maximum number of decimal places, since rational expansions may non-terminating.

hoogle , haddocks data.ratio didn't me. can find function?

here arbitrary precision solution doesn't use floats:

import data.ratio  display :: int -> rational -> string display len rat = (if num < 0 "-" else "") ++ (shows d ("." ++ take len (go next)))             (d, next) = abs num `quotrem` den         num = numerator rat         den = denominator rat          go 0 = ""         go x = let (d, next) = (10 * x) `quotrem` den                in shows d (go next) 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -