Skip to content
This repository was archived by the owner on Dec 18, 2020. It is now read-only.

Commit 607424a

Browse files
authored
Merge pull request #11 from kika/master
toFixed, toExponential, toPrecision Number methods implemented
2 parents d979b40 + 10af9fe commit 607424a

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/Global/Unsafe.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,22 @@
66
exports.unsafeStringify = function (x) {
77
return JSON.stringify(x);
88
};
9+
10+
exports.unsafeToFixed = function (digits) {
11+
return function (n) {
12+
return n.toFixed(digits);
13+
};
14+
};
15+
16+
exports.unsafeToExponential = function (digits) {
17+
return function (n) {
18+
return n.toExponential(digits);
19+
};
20+
};
21+
22+
exports.unsafeToPrecision = function (digits) {
23+
return function (n) {
24+
return n.toPrecision(digits);
25+
};
26+
};
27+

src/Global/Unsafe.purs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,20 @@ module Global.Unsafe where
33
-- | Uses the global JSON object to turn anything into a string. Careful! Trying
44
-- | to serialize functions returns undefined
55
foreign import unsafeStringify :: forall a. a -> String
6+
7+
-- | Formats Number as a String with limited number of digits after the dot
8+
-- | May throw RangeError if the number of digits is not within the allowed range
9+
-- | (standard precision range is 0 to 20, but implementations may change it)
10+
foreign import unsafeToFixed :: Int -> Number -> String
11+
12+
-- | Formats Number as String in exponential notation limiting number of digits
13+
-- | after the decimal dot.
14+
-- | May throw RangeError if the number of digits is not within the allowed range
15+
-- | (standard precision range is 0 to 20, but implementations may change it)
16+
foreign import unsafeToExponential :: Int -> Number -> String
17+
18+
-- | Formats Number as String in fixed-point or exponential notation rounded
19+
-- | to specified number of significant digits.
20+
-- | May throw RangeError if the number of digits is not within the allowed range
21+
-- | (standard precision range is 0 to 100, but implementations may change it)
22+
foreign import unsafeToPrecision :: Int -> Number -> String

test/Test/Main.purs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
module Test.Main where
22

3-
import Prelude (Unit, (==), ($), bind, not, negate, (<), (>), (/=))
3+
import Prelude
44

55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE, log)
77

8-
import Global
8+
import Global (readFloat, readInt, isFinite, infinity, nan, isNaN)
9+
import Global.Unsafe (unsafeToPrecision, unsafeToExponential, unsafeToFixed)
910

1011
import Test.Assert (ASSERT, assert)
1112

1213
main :: Eff (console :: CONSOLE, assert :: ASSERT) Unit
1314
main = do
15+
let num = 12345.6789
1416

1517
log "nan /= nan"
1618
assert $ nan /= nan
@@ -41,3 +43,21 @@ main = do
4143

4244
log "readFloat \"3.5\" == 3.5"
4345
assert $ readFloat "3.5" == 3.5
46+
47+
-- note the rounding
48+
log $ "unsafeToFixed 1" <> (show num) <> " == \"12345.7\""
49+
assert $ unsafeToFixed 1 num == "12345.7"
50+
51+
-- padded with zeros
52+
log $ "unsafeToFixed 6" <> (show num) <> " == \"12345.678900\""
53+
assert $ unsafeToFixed 6 num == "12345.678900"
54+
55+
log $ "unsafeToExponential 4" <> (show num) <> " == \"1.2346e+4\""
56+
assert $ unsafeToExponential 4 num == "1.2346e+4"
57+
58+
log $ "unsafeToPrecision 3" <> (show num) <> " == \"1.23e+4\""
59+
assert $ unsafeToPrecision 3 num == "1.23e+4"
60+
61+
log $ "unsafeToPrecision 6" <> (show num) <> " == \"12345.7\""
62+
assert $ unsafeToPrecision 6 num == "12345.7"
63+

0 commit comments

Comments
 (0)