format.js (6190B)
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 * Limit the number of digits of the formatted value. 56 * For regular numbers, must be a number between 0 and 16. 57 * For bignumbers, the maximum depends on the configured precision, 58 * see function `config()`. 59 * In case of notations 'exponential', 'engineering', and 'auto', `precision` 60 * defines the total number of significant digits returned. 61 * In case of notation 'fixed', `precision` defines the number of 62 * significant digits after the decimal point. 63 * `precision` is undefined by default. 64 * - `lowerExp: number` 65 * Exponent determining the lower boundary for formatting a value with 66 * an exponent when `notation='auto`. Default value is `-3`. 67 * - `upperExp: number` 68 * Exponent determining the upper boundary for formatting a value with 69 * an exponent when `notation='auto`. Default value is `5`. 70 * - `fraction: string`. Available values: 'ratio' (default) or 'decimal'. 71 * For example `format(fraction(1, 3))` will output '1/3' when 'ratio' is 72 * configured, and will output `0.(3)` when 'decimal' is configured. 73 * - `truncate: number`. Specifies the maximum allowed length of the 74 * returned string. If it would have been longer, the excess characters 75 * are deleted and replaced with `'...'`. 76 * - `callback: function` 77 * A custom formatting function, invoked for all numeric elements in `value`, 78 * for example all elements of a matrix, or the real and imaginary 79 * parts of a complex number. This callback can be used to override the 80 * built-in numeric notation with any type of formatting. Function `callback` 81 * is called with `value` as parameter and must return a string. 82 * 83 * When `value` is an Object: 84 * 85 * - When the object contains a property `format` being a function, this function 86 * is invoked as `value.format(options)` and the result is returned. 87 * - When the object has its own `toString` method, this method is invoked 88 * and the result is returned. 89 * - In other cases the function will loop over all object properties and 90 * return JSON object notation like '{"a": 2, "b": 3}'. 91 * 92 * When value is a function: 93 * 94 * - When the function has a property `syntax`, it returns this 95 * syntax description. 96 * - In other cases, a string `'function'` is returned. 97 * 98 * Examples: 99 * 100 * math.format(6.4) // returns '6.4' 101 * math.format(1240000) // returns '1.24e6' 102 * math.format(1/3) // returns '0.3333333333333333' 103 * math.format(1/3, 3) // returns '0.333' 104 * math.format(21385, 2) // returns '21000' 105 * math.format(12e8, {notation: 'fixed'}) // returns '1200000000' 106 * math.format(2.3, {notation: 'fixed', precision: 4}) // returns '2.3000' 107 * math.format(52.8, {notation: 'exponential'}) // returns '5.28e+1' 108 * math.format(12400,{notation: 'engineering'}) // returns '12.400e+3' 109 * math.format(2000, {lowerExp: -2, upperExp: 2}) // returns '2e+3' 110 * 111 * function formatCurrency(value) { 112 * // return currency notation with two digits: 113 * return '$' + value.toFixed(2) 114 * 115 * // you could also use math.format inside the callback: 116 * // return '$' + math.format(value, {notation: 'fixed', precision: 2}) 117 * } 118 * math.format([2.1, 3, 0.016], formatCurrency) // returns '[$2.10, $3.00, $0.02]' 119 * 120 * See also: 121 * 122 * print 123 * 124 * @param {*} value Value to be stringified 125 * @param {Object | Function | number} [options] Formatting options 126 * @return {string} The formatted value 127 */ 128 return typed(name, { 129 any: formatString, 130 'any, Object | function | number': formatString 131 }); 132 });