包详细信息

numeric-quantity

jakeboone02196.3kMIT2.1.0

Number parser with support for mixed numbers, vulgar fractions, and Roman numerals

parse, number, convert, fraction

自述文件

npm workflow status codecov.io downloads MIT License

Converts a string to a number, like an enhanced version of parseFloat.

Full documentation

Features:

  • In addition to plain integers and decimals, numeric-quantity can parse numbers with comma or underscore separators ('1,000' or '1_000'), mixed numbers ('1 2/3'), vulgar fractions ('1⅖'), and the fraction slash character ('1 2⁄3').
  • To allow and ignore trailing invalid characters à la parseFloat, pass { allowTrailingInvalid: true } as the second argument.
  • To parse Roman numerals like 'MCCXIV' or 'Ⅻ', pass { romanNumerals: true } as the second argument or call parseRomanNumerals directly.
  • To produce bigint values when the input represents an integer that would exceeds the boundaries of number, pass { bigIntOnOverflow: true } as the second argument.
  • Results will be rounded to three decimal places by default. To avoid rounding, pass { round: false } as the second argument. To round to a different number of decimal places, assign that number to the round option ({ round: 5 } will round to five decimal places).
  • Returns NaN if the provided string does not resemble a number.

For the inverse operation—converting a number to an imperial measurement—check out format-quantity.

For a more complete solution to parsing recipe ingredients, try parse-ingredient.

Usage

Installed

import { numericQuantity } from 'numeric-quantity';

console.log(numericQuantity('1 1/2')); // 1.5
console.log(numericQuantity('2 2/3')); // 2.667

CDN

As an ES module:

<script type="module">
  import { numericQuantity } from 'https://cdn.jsdelivr.net/npm/numeric-quantity/+esm';

  console.log(numericQuantity('10½')); // 10.5
</script>

As UMD (all exports are properties of the global object NumericQuantity):

<script src="https://unpkg.com/numeric-quantity"></script>
<script>
  console.log(NumericQuantity.numericQuantity('xii', { romanNumerals: true })); // 12
</script>

更新日志

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Unreleased

  • N/A

v2.1.0 - 2025-06-09

Added

  • bigIntOnOverflow option will produce a bigint value if the input represents a valid integer greater than Number.MAX_SAFE_INTEGER or less than Number.MIN_SAFE_INTEGER.

v2.0.1 - 2024-01-15

Fixed

  • Corrected links in package.json to distributed type definition files.

v2.0.0 - 2023-06-16

Changed

  • #26 numericQuantity is now a named export; there is no default export.
  • #26 UMD build assigns all exports, including numericQuantity, to the global object NumericQuantity. Previously, it assigned the main function to the global namespace as numericQuantity.

Added

  • #26 Support for comma (',') and underscore ('_') separators within Arabic numeral sequences. If a numeric sequence has a leading or trailing separator, that sequence will be considered invalid. This will cause numericQuantity to return NaN unless allowTrailingInvalid is true (see next item), in which case the sequence in question and everything after it will be ignored.
  • #26 Options object as optional second parameter. Accepts the following options:
    • allowTrailingInvalid (boolean, default false): Allows numericQuantity to more closely resemble the behavior of parseFloat by accepting and ignoring everything from the first invalid character to the end of the string.
    • romanNumerals (boolean, default false): Enables support for Roman numerals with modern, strict rules, including the Unicode code points U+2160 through U+217F. Roman numerals will only be parsed if an attempt to parse the string based on Arabic numerals fails. To parse Roman numerals unconditionally, call parseRomanNumerals directly.
    • round (number | false, default 3): Rounds the result to the specified number of decimal places. Use round: false to avoid rounding.
  • #26 Support for Unicode "Fraction Numerator One" code point ('⅟', U+215F), which must be followed by a numeric sequence (the denominator) to be considered part of a valid fraction representation.
  • #26 Named exports of internal utilities like regular expressions, character maps, types, etc.
  • #26 Build with tsup.

v1.0.4 - 2022-04-16

Fixed

v1.0.3 - 2022-04-16

Added

v1.0.2 - 2021-08-23

Added

  • #21 Support for Unicode fraction slash (, U+2044).

v1.0.1 - 2021-02-15

Fixed

v1.0.0 - 2021-02-11

Added

v0.5.2 - 2021-02-08

Fixed

v0.5.1 - 2019-08-24

Fixed

v0.5.0 - 2019-08-24

Changed

  • Returns NaN for invalid inputs instead of -1.

Fixed

  • #3 Handles negative numbers.

v0.4.2 - 2019-08-23

Fixed

  • Publish dist directory only.

v0.4.1 - 2019-08-23

Changed

  • Rewritten in TypeScript.

Added

v0.4.0 - 2019-08-22

Added

  • ESM and CJS builds.

v0.3.3 - 2019-07-21

Added

  • JSDoc comments for tooltips

v0.3.2 - 2018-09-21

Added

  • TypeScript types.

v0.3.1 - 2015-07-16

Fixed

  • Documentation update.

v0.3.0 - 2015-07-16

Added

  • UMD support.

Fixed

  • Minor bug fixes.

v0.2.0 - 2015-05-14

Added

  • #1 Accept decimals without a leading zero.

v0.1.2 - 2015-03-20

Fixed

  • Minor performance improvement.

v0.1.1 - 2015-03-19

Changed

  • Use parseInt/parseFloat instead of str - 0 to parse numbers from strings.

v0.1.0 - 2015-03-18

Added

  • Initial release.