File: HumanNumber.hs

package info (click to toggle)
git-annex 8.20210223-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 68,764 kB
  • sloc: haskell: 70,359; javascript: 9,103; sh: 1,304; makefile: 212; perl: 136; ansic: 44
file content (21 lines) | stat: -rw-r--r-- 683 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{- numbers for humans
 -
 - Copyright 2012-2013 Joey Hess <id@joeyh.name>
 -
 - License: BSD-2-clause
 -}

module Utility.HumanNumber (showImprecise) 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 = replicate (precision - length s) '0' ++ s
	striptrailing0s = reverse . dropWhile (== '0') . reverse