1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
module TextBuilderDev.Domains.Other where
import TextBuilder
import TextBuilderDev.Domains.Digits
import TextBuilderDev.Prelude hiding (intercalate)
-- | Data size in decimal notation over amount of bytes.
--
-- >>> approximateDataSize 999
-- "999B"
--
-- >>> approximateDataSize 9999
-- "9.9kB"
--
-- >>> approximateDataSize (-9999)
-- "-9.9kB"
--
-- >>> approximateDataSize 1234567890
-- "1.2GB"
--
-- >>> approximateDataSize 10000000000000000000000000000000023
-- "10,000,000,000YB"
{-# INLINEABLE approximateDataSize #-}
approximateDataSize :: (Integral a) => a -> TextBuilder
approximateDataSize = signed \a ->
if a < 1000
then decimal a <> "B"
else
if a < 1000000
then dividedDecimal 100 a <> "kB"
else
if a < 1000000000
then dividedDecimal 100000 a <> "MB"
else
if a < 1000000000000
then dividedDecimal 100000000 a <> "GB"
else
if a < 1000000000000000
then dividedDecimal 100000000000 a <> "TB"
else
if a < 1000000000000000000
then dividedDecimal 100000000000000 a <> "PB"
else
if a < 1000000000000000000000
then dividedDecimal 100000000000000000 a <> "EB"
else
if a < 1000000000000000000000000
then dividedDecimal 100000000000000000000 a <> "ZB"
else dividedDecimal 100000000000000000000000 a <> "YB"
where
dividedDecimal divisor n =
let byDivisor = div n divisor
byExtraTen = div byDivisor 10
remainder = byDivisor - byExtraTen * 10
separatorChar = ','
in if remainder == 0 || byExtraTen >= 10
then thousandSeparatedDecimal separatorChar byExtraTen
else thousandSeparatedDecimal separatorChar byExtraTen <> "." <> decimalDigit remainder
-- | Double with a fixed number of decimal places.
--
-- >>> doubleFixedPoint 4 0.123456
-- "0.1235"
--
-- >>> doubleFixedPoint 2 2.1
-- "2.10"
--
-- >>> doubleFixedPoint (-2) 2.1
-- "2"
--
-- >>> doubleFixedPoint 2 (-2.1)
-- "-2.10"
--
-- >>> doubleFixedPoint 2 0
-- "0.00"
{-# INLINE doubleFixedPoint #-}
doubleFixedPoint ::
-- | Amount of decimals after point.
Int ->
Double ->
TextBuilder
doubleFixedPoint (max 0 -> decimalPlaces) =
fromString . printf ("%." ++ show decimalPlaces ++ "f")
-- | Double multiplied by 100 with a fixed number of decimal places applied and followed by a percent-sign.
--
-- >>> doubleFixedPointPercent 3 0.123456
-- "12.346%"
--
-- >>> doubleFixedPointPercent 0 2
-- "200%"
--
-- >>> doubleFixedPointPercent 0 (-2)
-- "-200%"
{-# INLINE doubleFixedPointPercent #-}
doubleFixedPointPercent ::
-- | Amount of decimals after point.
Int ->
Double ->
TextBuilder
doubleFixedPointPercent decimalPlaces x = doubleFixedPoint decimalPlaces (x * 100) <> "%"
|