From 4399097564c041838140225f30dcda7f92caa88a Mon Sep 17 00:00:00 2001 From: Joris Date: Wed, 10 May 2017 10:29:30 +0200 Subject: Add tests for purescript number and parser utilities --- js/src/Number.purs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 js/src/Number.purs (limited to 'js/src/Number.purs') diff --git a/js/src/Number.purs b/js/src/Number.purs new file mode 100644 index 0000000..0403f19 --- /dev/null +++ b/js/src/Number.purs @@ -0,0 +1,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 -- cgit v1.2.3