format.js 6.2 KB

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