1 | import { format as formatString } from '../../utils/string.js';
|
2 | import { factory } from '../../utils/factory.js';
|
3 | var name = 'format';
|
4 | var dependencies = ['typed'];
|
5 | export var createFormat = /* #__PURE__ */factory(name, dependencies, (_ref) => {
|
6 | var {
|
7 | typed
|
8 | } = _ref;
|
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: always have exponential notation,
|
36 | * and select the exponent to be a multiple of 3.
|
37 | * For example '123.4e+0' and '14.0e+6'
|
38 | * - 'auto' (default)
|
39 | * Regular number notation for numbers having an absolute value between
|
40 | * `lower` and `upper` bounds, and uses exponential notation elsewhere.
|
41 | * Lower bound is included, upper bound is excluded.
|
42 | * For example '123.4' and '1.4e7'.
|
43 | * - 'bin', 'oct, or 'hex'
|
44 | * Format the number using binary, octal, or hexadecimal notation.
|
45 | * For example '0b1101' and '0x10fe'.
|
46 | * - `wordSize: number`
|
47 | * The word size in bits to use for formatting in binary, octal, or
|
48 | * hexadecimal notation. To be used only with 'bin', 'oct', or 'hex'
|
49 | * values for 'notation' option. When this option is defined the value
|
50 | * is formatted as a signed twos complement integer of the given word
|
51 | * size and the size suffix is appended to the output.
|
52 | * For example format(-1, {notation: 'hex', wordSize: 8}) === '0xffi8'.
|
53 | * Default value is undefined.
|
54 | * - `precision: number`
|
55 | * A number between 0 and 16 to round the digits of the number. In case
|
56 | * of notations 'exponential', 'engineering', and 'auto', `precision`
|
57 | * defines the total number of significant digits returned.
|
58 | * In case of notation 'fixed', `precision` defines the number of
|
59 | * significant digits after the decimal point.
|
60 | * `precision` is undefined by default.
|
61 | * - `lowerExp: number`
|
62 | * Exponent determining the lower boundary for formatting a value with
|
63 | * an exponent when `notation='auto`. Default value is `-3`.
|
64 | * - `upperExp: number`
|
65 | * Exponent determining the upper boundary for formatting a value with
|
66 | * an exponent when `notation='auto`. Default value is `5`.
|
67 | * - `fraction: string`. Available values: 'ratio' (default) or 'decimal'.
|
68 | * For example `format(fraction(1, 3))` will output '1/3' when 'ratio' is
|
69 | * configured, and will output `0.(3)` when 'decimal' is configured.
|
70 | * - `callback: function`
|
71 | * A custom formatting function, invoked for all numeric elements in `value`,
|
72 | * for example all elements of a matrix, or the real and imaginary
|
73 | * parts of a complex number. This callback can be used to override the
|
74 | * built-in numeric notation with any type of formatting. Function `callback`
|
75 | * is called with `value` as parameter and must return a string.
|
76 | *
|
77 | * When `value` is an Object:
|
78 | *
|
79 | * - When the object contains a property `format` being a function, this function
|
80 | * is invoked as `value.format(options)` and the result is returned.
|
81 | * - When the object has its own `toString` method, this method is invoked
|
82 | * and the result is returned.
|
83 | * - In other cases the function will loop over all object properties and
|
84 | * return JSON object notation like '{"a": 2, "b": 3}'.
|
85 | *
|
86 | * When value is a function:
|
87 | *
|
88 | * - When the function has a property `syntax`, it returns this
|
89 | * syntax description.
|
90 | * - In other cases, a string `'function'` is returned.
|
91 | *
|
92 | * Examples:
|
93 | *
|
94 | * math.format(6.4) // returns '6.4'
|
95 | * math.format(1240000) // returns '1.24e6'
|
96 | * math.format(1/3) // returns '0.3333333333333333'
|
97 | * math.format(1/3, 3) // returns '0.333'
|
98 | * math.format(21385, 2) // returns '21000'
|
99 | * math.format(12e8, {notation: 'fixed'}) // returns '1200000000'
|
100 | * math.format(2.3, {notation: 'fixed', precision: 4}) // returns '2.3000'
|
101 | * math.format(52.8, {notation: 'exponential'}) // returns '5.28e+1'
|
102 | * math.format(12400,{notation: 'engineering'}) // returns '12.400e+3'
|
103 | * math.format(2000, {lowerExp: -2, upperExp: 2}) // returns '2e+3'
|
104 | *
|
105 | * function formatCurrency(value) {
|
106 | * // return currency notation with two digits:
|
107 | * return '$' + value.toFixed(2)
|
108 | *
|
109 | * // you could also use math.format inside the callback:
|
110 | * // return '$' + math.format(value, {notation: 'fixed', precision: 2})
|
111 | * }
|
112 | * math.format([2.1, 3, 0.016], formatCurrency} // returns '[$2.10, $3.00, $0.02]'
|
113 | *
|
114 | * See also:
|
115 | *
|
116 | * print
|
117 | *
|
118 | * @param {*} value Value to be stringified
|
119 | * @param {Object | Function | number} [options] Formatting options
|
120 | * @return {string} The formatted value
|
121 | */
|
122 | return typed(name, {
|
123 | any: formatString,
|
124 | 'any, Object | function | number': formatString
|
125 | });
|
126 | }); |
\ | No newline at end of file |