format.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * Format a value of any type into a string.
  11. *
  12. * Syntax:
  13. *
  14. * math.format(value)
  15. * math.format(value, options)
  16. * math.format(value, precision)
  17. * math.format(value, callback)
  18. *
  19. * Where:
  20. *
  21. * - `value: *`
  22. * The value to be formatted
  23. * - `options: Object`
  24. * An object with formatting options. Available options:
  25. * - `notation: string`
  26. * Number notation. Choose from:
  27. * - `'fixed'`
  28. * Always use regular number notation.
  29. * For example `'123.40'` and `'14000000'`
  30. * - `'exponential'`
  31. * Always use exponential notation.
  32. * For example `'1.234e+2'` and `'1.4e+7'`
  33. * - `'engineering'`
  34. * Always use engineering notation: always have exponential notation,
  35. * and select the exponent to be a multiple of `3`.
  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. * - `'bin'`, `'oct'`, or `'hex'`
  43. * Format the number using binary, octal, or hexadecimal notation.
  44. * For example `'0b1101'` and `'0x10fe'`.
  45. * - `wordSize: number`
  46. * The word size in bits to use for formatting in binary, octal, or
  47. * hexadecimal notation. To be used only with `'bin'`, `'oct'`, or `'hex'`
  48. * values for `notation` option. When this option is defined the value
  49. * is formatted as a signed twos complement integer of the given word
  50. * size and the size suffix is appended to the output.
  51. * For example `format(-1, {notation: 'hex', wordSize: 8}) === '0xffi8'`.
  52. * Default value is undefined.
  53. * - `precision: number`
  54. * Limit the number of digits of the formatted value.
  55. * For regular numbers, must be a number between `0` and `16`.
  56. * For bignumbers, the maximum depends on the configured precision,
  57. * see function `config()`.
  58. * In case of notations `'exponential'`, `'engineering'`, and `'auto'`,
  59. * `precision` defines the total number of significant digits returned.
  60. * In case of notation `'fixed'`, `precision` defines the number of
  61. * significant digits after the decimal point.
  62. * `precision` is undefined by default.
  63. * - `lowerExp: number`
  64. * Exponent determining the lower boundary for formatting a value with
  65. * an exponent when `notation='auto'`. Default value is `-3`.
  66. * - `upperExp: number`
  67. * Exponent determining the upper boundary for formatting a value with
  68. * an exponent when `notation='auto'`. Default value is `5`.
  69. * - `fraction: string`. Available values: `'ratio'` (default) or `'decimal'`.
  70. * For example `format(fraction(1, 3))` will output `'1/3'` when `'ratio'`
  71. * is configured, and will output `'0.(3)'` when `'decimal'` is configured.
  72. * - `truncate: number`. Specifies the maximum allowed length of the
  73. * returned string. If it had been longer, the excess characters
  74. * are deleted and replaced with `'...'`.
  75. * - `callback: function`
  76. * A custom formatting function, invoked for all numeric elements in `value`,
  77. * for example all elements of a matrix, or the real and imaginary
  78. * parts of a complex number. This callback can be used to override the
  79. * built-in numeric notation with any type of formatting. Function `callback`
  80. * is called with `value` as parameter and must return a string.
  81. *
  82. * When `value` is an Object:
  83. *
  84. * - When the object contains a property `format` being a function, this function
  85. * is invoked as `value.format(options)` and the result is returned.
  86. * - When the object has its own `toString` method, this method is invoked
  87. * and the result is returned.
  88. * - In other cases the function will loop over all object properties and
  89. * return JSON object notation like '{"a": 2, "b": 3}'.
  90. *
  91. * When value is a function:
  92. *
  93. * - When the function has a property `syntax`, it returns this
  94. * syntax description.
  95. * - In other cases, a string `'function'` is returned.
  96. *
  97. * Examples:
  98. *
  99. * math.format(6.4) // returns '6.4'
  100. * math.format(1240000) // returns '1.24e+6'
  101. * math.format(1/3) // returns '0.3333333333333333'
  102. * math.format(1/3, 3) // returns '0.333'
  103. * math.format(21385, 2) // returns '21000'
  104. * math.format(12e8, {notation: 'fixed'}) // returns '1200000000'
  105. * math.format(2.3, {notation: 'fixed', precision: 4}) // returns '2.3000'
  106. * math.format(52.8, {notation: 'exponential'}) // returns '5.28e+1'
  107. * math.format(12400, {notation: 'engineering'}) // returns '12.4e+3'
  108. * math.format(2000, {lowerExp: -2, upperExp: 2}) // returns '2e+3'
  109. *
  110. * function formatCurrency(value) {
  111. * // return currency notation with two digits:
  112. * return '$' + value.toFixed(2)
  113. *
  114. * // you could also use math.format inside the callback:
  115. * // return '$' + math.format(value, {notation: 'fixed', precision: 2})
  116. * }
  117. * math.format([2.1, 3, 0.016], formatCurrency) // returns '[$2.10, $3.00, $0.02]'
  118. *
  119. * See also:
  120. *
  121. * print
  122. *
  123. * @param {*} value Value to be stringified
  124. * @param {Object | Function | number} [options] Formatting options
  125. * @return {string} The formatted value
  126. */
  127. return typed(name, {
  128. any: formatString,
  129. 'any, Object | function | number': formatString
  130. });
  131. });