Data.Formatter.Number
- Package
- purescript-formatters
- Repository
- purescript-contrib/purescript-formatters
Formatter has following properties
- Number of digits before dot
- Number of digits after dot
- Should sign be printed for positive numbers
- Should thousands be separated by comma
- Should output string have abbreviations (like
KorM) - What decimal-separator character should be used (default '.')
- What thousand-group-separator character should be used (default '+')
Note: The parser will return a formatter with the default separator-characters - use withSeparators to override this after parsing.
Number will be padded with zeros to have at least this number of leading zeros. This doesn't restrict number to have more digits then leading zeros in format string.
0000.0will show 4 digits:12 → "0012.0",1234 → "1234.0"00.0will show only 2 digits :12 → "12.0",1234 → "1234.0"
Number of digits after dot is set by number of trailing zeros (note the rounding)
0.000will show 3 digits:0.12345 → "0.123",12.98765 → "12.988"0.0will show only 1 digit:0.12345 → "0.1",12.98765 → "13.0"
If number is lesser then zero - is always printed. Otherwise you could specify + in format string
+0:12.0 → "+12",-34.8 → "-35"0:12.0 → "12",-34.8 → "-35"
Thousands separator is specified as ,0 please note that this 0 isn't counted as leading.
00,0:1234567890 → "1,234,567,890.0",1 → "1.0"`
For abbreviation one could use a flag. In general it tries to find the closest power of thousand and
then use formatter to result of division of input number and that power.
0a:1234567 → "1M",1 → "1"
This module has no support of percents and currencies.
Please, note that using simple formatter that tabulates number with
zeros and put commas between thousands should be enough for everything
because one could just compose it with flip append "%" or whatever.
#Formatter Source
newtype FormatterDefines a format for printing/parsing numbers.
comma: use a ',' for a thousands separator
before: the minimum number of characters to print before the decimal point
after: the total number of characters to print after the decimal point
abbreviations: "31600.0" → "32K"; "31600000.0" → "32M"
sign: always print a sign, including a + for positive numbers
Constructors
Formatter { abbreviations :: Boolean, after :: Int, before :: Int, comma :: Boolean, decimalSeparator :: Char, groupSeparator :: Char, sign :: Boolean }
Instances
#withSeparators Source
withSeparators :: { decimalSeparator :: Char, groupSeparator :: Char } -> Formatter -> Formatterchange the default Separators for example for german formatting you could do
parseFormatString ".." # map (withSeparators { groupSeparator: '.', decimalSeparator: ','})
#printFormatter Source
printFormatter :: Formatter -> StringThe format string representation of a Formatter.
#parseFormatString Source
parseFormatString :: String -> Either String FormatterAttempt to parse a String as a Formatter,
using an interpretation inspired by numeral.js
#format Source
format :: Formatter -> Number -> StringFormat a Number according to the Formatter provided.
Due to the nature of floating point numbers, may yield unpredictable results for extremely
large or extremely small numbers, such as numbers whose absolute values are ≥ 1e21 or ≤ 1e-21,
or when formatting with > 20 digits after the decimal place.
See purescript-decimals
for working with arbitrary precision decimals, which supports simple number
formatting for numbers that go beyond the precision available with Number.
#unformat Source
unformat :: Formatter -> String -> Either String NumberAttempt to parse a String as a Number according to the format defined in the
given Formatter.
The digit counts are minimum widths: additional fractional digits are retained.
Parsing uses the precision of Number, without an intermediate 32-bit Int.
#formatNumber Source
formatNumber :: String -> Number -> Either String StringFormat a Number according to the format defined in the given format string.
If the format string fails to parse, will return a Left value.
The interpretation of the format string is inspired by numeral.js
#unformatNumber Source
unformatNumber :: String -> String -> Either String NumberAttempt to parse a String as a Number according to the format defined in the
given format string. Returns a Left value if the given format string fails to parse, or
if the number string fails to parse according to the format.
The interpretation of the format string is inspired by numeral.js