aboutsummaryrefslogtreecommitdiff
path: root/js/src/Number.purs
blob: 0403f19190407c827b94055225b210c80eff6120 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module Number
  ( format
  , roundAt
  ) where

import Data.Int (round, toNumber, pow) as Int
import Data.String (Pattern(..), Replacement(..))
import Data.String (replace) as String
import Math (round) as Math
import Prelude

format :: Number -> String
format number =
  if Math.round number == number then
    show (Int.round number)
  else
    String.replace (Pattern ".") (Replacement ",") (show (roundAt 1 number))

roundAt :: Int -> Number -> Number
roundAt at n =
  let exp = Int.toNumber (Int.pow 10 at)
  in  Math.round (n * exp) / exp