1 | import { format as formatString } from '../../utils/string';
|
2 | import { factory } from '../../utils/factory';
|
3 | var name = 'format';
|
4 | var dependencies = ['typed'];
|
5 | export var createFormat =
|
6 | /* #__PURE__ */
|
7 | factory(name, dependencies, function (_ref) {
|
8 | var typed = _ref.typed;
|
9 |
|
10 | /**
|
11 | * Format a value of any type into a string.
|
12 | *
|
13 | * Syntax:
|
14 | *
|
15 | * math.format(value)
|
16 | * math.format(value, options)
|
17 | * math.format(value, precision)
|
18 | * math.format(value, callback)
|
19 | *
|
20 | * Where:
|
21 | *
|
22 | * - `value: *`
|
23 | * The value to be formatted
|
24 | * - `options: Object`
|
25 | * An object with formatting options. Available options:
|
26 | * - `notation: string`
|
27 | * Number notation. Choose from:
|
28 | * - 'fixed'
|
29 | * Always use regular number notation.
|
30 | * For example '123.40' and '14000000'
|
31 | * - 'exponential'
|
32 | * Always use exponential notation.
|
33 | * For example '1.234e+2' and '1.4e+7'
|
34 | * - 'engineering'
|
35 | * Always use engineering notation.
|
36 | * For example '123.4e+0' and '14.0e+6'
|
37 | * - 'auto' (default)
|
38 | * Regular number notation for numbers having an absolute value between
|
39 | * `lower` and `upper` bounds, and uses exponential notation elsewhere.
|
40 | * Lower bound is included, upper bound is excluded.
|
41 | * For example '123.4' and '1.4e7'.
|
42 | * - `precision: number`
|
43 | * A number between 0 and 16 to round the digits of the number. In case
|
44 | * of notations 'exponential', 'engineering', and 'auto', `precision`
|
45 | * defines the total number of significant digits returned.
|
46 | * In case of notation 'fixed', `precision` defines the number of
|
47 | * significant digits after the decimal point.
|
48 | * `precision` is undefined by default.
|
49 | * - `lowerExp: number`
|
50 | * Exponent determining the lower boundary for formatting a value with
|
51 | * an exponent when `notation='auto`. Default value is `-3`.
|
52 | * - `upperExp: number`
|
53 | * Exponent determining the upper boundary for formatting a value with
|
54 | * an exponent when `notation='auto`. Default value is `5`.
|
55 | * - `fraction: string`. Available values: 'ratio' (default) or 'decimal'.
|
56 | * For example `format(fraction(1, 3))` will output '1/3' when 'ratio' is
|
57 | * configured, and will output `0.(3)` when 'decimal' is configured.
|
58 | * - `callback: function`
|
59 | * A custom formatting function, invoked for all numeric elements in `value`,
|
60 | * for example all elements of a matrix, or the real and imaginary
|
61 | * parts of a complex number. This callback can be used to override the
|
62 | * built-in numeric notation with any type of formatting. Function `callback`
|
63 | * is called with `value` as parameter and must return a string.
|
64 | *
|
65 | * When `value` is an Object:
|
66 | *
|
67 | * - When the object contains a property `format` being a function, this function
|
68 | * is invoked as `value.format(options)` and the result is returned.
|
69 | * - When the object has its own `toString` method, this method is invoked
|
70 | * and the result is returned.
|
71 | * - In other cases the function will loop over all object properties and
|
72 | * return JSON object notation like '{"a": 2, "b": 3}'.
|
73 | *
|
74 | * When value is a function:
|
75 | *
|
76 | * - When the function has a property `syntax`, it returns this
|
77 | * syntax description.
|
78 | * - In other cases, a string `'function'` is returned.
|
79 | *
|
80 | * Examples:
|
81 | *
|
82 | * math.format(6.4) // returns '6.4'
|
83 | * math.format(1240000) // returns '1.24e6'
|
84 | * math.format(1/3) // returns '0.3333333333333333'
|
85 | * math.format(1/3, 3) // returns '0.333'
|
86 | * math.format(21385, 2) // returns '21000'
|
87 | * math.format(12e8, {notation: 'fixed'}) // returns '1200000000'
|
88 | * math.format(2.3, {notation: 'fixed', precision: 4}) // returns '2.3000'
|
89 | * math.format(52.8, {notation: 'exponential'}) // returns '5.28e+1'
|
90 | * math.format(12400,{notation: 'engineering'}) // returns '12.400e+3'
|
91 | * math.format(2000, {lowerExp: -2, upperExp: 2}) // returns '2e+3'
|
92 | *
|
93 | * function formatCurrency(value) {
|
94 | * // return currency notation with two digits:
|
95 | * return '$' + value.toFixed(2)
|
96 | *
|
97 | * // you could also use math.format inside the callback:
|
98 | * // return '$' + math.format(value, {notation: 'fixed', precision: 2})
|
99 | * }
|
100 | * math.format([2.1, 3, 0.016], formatCurrency} // returns '[$2.10, $3.00, $0.02]'
|
101 | *
|
102 | * See also:
|
103 | *
|
104 | * print
|
105 | *
|
106 | * @param {*} value Value to be stringified
|
107 | * @param {Object | Function | number} [options] Formatting options
|
108 | * @return {string} The formatted value
|
109 | */
|
110 | return typed(name, {
|
111 | any: formatString,
|
112 | 'any, Object | function | number': formatString
|
113 | });
|
114 | }); |
\ | No newline at end of file |