aboutsummaryrefslogtreecommitdiff
path: root/src/Format.purs
blob: 22593e52f36e19b07bc482f31d7c318618317791 (plain)
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
module Format where

import Data.Int (toNumber, fromNumber)
import Data.Array (replicate)
import Data.Maybe (fromMaybe)
import Data.String (length, joinWith)
import Math (round, trunc, pow)

import Prelude

number :: Int -> Number -> String
number decimalLength num = formattedIntegerPart <> formattedDecimalPart
  where
  formattedIntegerPart =
    (if decimalLength > 0 then trunc num else round num)
      # fromNumber
      # fromMaybe 0
      # show

  formattedDecimalPart =
    if decimalLength > 0 then
      ((num - trunc num) * pow 10.0 (toNumber decimalLength))
        # round
        # fromNumber
        # fromMaybe 0
        # show
        # \str -> "," <> (joinWith "" $ replicate (decimalLength - length str) "0") <> str
    else
      ""