refactor and unify code
This fixes several bugs in both modules.
This commit is contained in:
parent
442418d845
commit
9c68e06276
3 changed files with 37 additions and 27 deletions
21
Utility/HumanNumber.hs
Normal file
21
Utility/HumanNumber.hs
Normal file
|
@ -0,0 +1,21 @@
|
|||
{- numbers for humans
|
||||
-
|
||||
- Copyright 2012-2013 Joey Hess <joey@kitenet.net>
|
||||
-
|
||||
- Licensed under the GNU GPL version 3 or higher.
|
||||
-}
|
||||
|
||||
module Utility.HumanNumber where
|
||||
|
||||
{- Displays a fractional value as a string with a limited number
|
||||
- of decimal digits. -}
|
||||
showImprecise :: RealFrac a => Int -> a -> String
|
||||
showImprecise precision n
|
||||
| precision == 0 || remainder == 0 = show (round n :: Integer)
|
||||
| otherwise = show int ++ "." ++ striptrailing0s (pad0s $ show remainder)
|
||||
where
|
||||
int :: Integer
|
||||
(int, frac) = properFraction n
|
||||
remainder = round (frac * 10 ^ precision) :: Integer
|
||||
pad0s s = (take (precision - length s) (repeat '0')) ++ s
|
||||
striptrailing0s = reverse . dropWhile (== '0') . reverse
|
Loading…
Add table
Add a link
Reference in a new issue