forked from purescript/purescript-numbers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormat.purs
More file actions
76 lines (68 loc) · 2.09 KB
/
Copy pathFormat.purs
File metadata and controls
76 lines (68 loc) · 2.09 KB
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
-- | A module for formatting numbers as strings.
-- |
-- | Usage:
-- | ``` purs
-- | > let x = 1234.56789
-- |
-- | > toStringWith (precision 6) x
-- | "1234.57"
-- |
-- | > toStringWith (fixed 3) x
-- | "1234.568"
-- |
-- | > toStringWith (exponential 2) x
-- | "1.23e+3"
-- | ```
-- |
-- | The main method of this module is the `toStringWith` function that accepts
-- | a `Format` argument which can be constructed through one of the smart
-- | constructors `precision`, `fixed` and `exponential`. Internally, the
-- | number will be formatted with JavaScripts `toPrecision`, `toFixed` or
-- | `toExponential`.
module Data.Number.Format
( Format()
, precision
, fixed
, exponential
, toStringWith
, toString
) where
import Prelude
foreign import toPrecisionNative :: Int -> Number -> String
foreign import toFixedNative :: Int -> Number -> String
foreign import toExponentialNative :: Int -> Number -> String
-- | The `Format` data type specifies how a number will be formatted.
data Format
= Precision Int
| Fixed Int
| Exponential Int
-- | Create a `toPrecision`-based format from an integer. Values smaller than
-- | `1` and larger than `21` will be clamped.
precision :: Int -> Format
precision = Precision <<< clamp 1 21
-- | Create a `toFixed`-based format from an integer. Values smaller than `0`
-- | and larger than `20` will be clamped.
fixed :: Int -> Format
fixed = Fixed <<< clamp 0 20
-- | Create a `toExponential`-based format from an integer. Values smaller than
-- | `0` and larger than `20` will be clamped.
exponential :: Int -> Format
exponential = Exponential <<< clamp 0 20
-- | Convert a number to a string with a given format.
toStringWith :: Format -> Number -> String
toStringWith (Precision p) = toPrecisionNative p
toStringWith (Fixed p) = toFixedNative p
toStringWith (Exponential p) = toExponentialNative p
-- | Convert a number to a string via JavaScript's toString method.
-- |
-- | ```purs
-- | > toString 12.34
-- | "12.34"
-- |
-- | > toString 1234.0
-- | "1234"
-- |
-- | > toString 1.2e-10
-- | "1.2e-10"
-- | ```
foreign import toString :: Number -> String