Package Exports
- numeric-quantity
- numeric-quantity/package.json
Readme
numeric-quantity
Converts a string to a number, like an enhanced version of parseFloat
.
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⅖'
), the fraction slash character ('1 2⁄3'
), and Roman numerals ('MCCXIV'
or 'Ⅻ'
). The return value will be 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')); // 12
</script>
Other exports
Name | Type | Description |
---|---|---|
numericRegex |
RegExp |
Regular expression matching a string that resembles a number (using Arabic numerals) in its entirety |
VulgarFraction |
type |
Union type of all unicode vulgar fraction code points |
vulgarFractionsRegex |
RegExp |
Regular expression matching the first unicode vulgar fraction code point |
vulgarFractionToAsciiMap |
object |
Mapping of each vulgar fraction to its traditional ASCII representation (e.g., '½' to '1/2' ) |
parseRomanNumerals |
function |
Same function signature as numericQuantity , but only for Roman numerals (used internally) |
romanNumeralRegex |
RegExp |
Regular expression matching valid Roman numeral sequences (uses modern, strict rules) |
romanNumeralUnicodeRegex |
RegExp |
Regular expression matching any unicode Roman numeral code point |
romanNumeralUnicodeToAsciiMap |
object |
Mapping of each Roman numeral to its traditional ASCII representation (e.g., 'Ⅻ' to 'XII' ) |
romanNumeralValues |
object |
Mapping of each valid Roman numeral sequence fragment to its numeric value |
RomanNumeralAscii |
type |
Union type of allowable Roman numeral characters (uppercase only) |
RomanNumeralUnicode |
type |
Union type of all Unicode Roman numeral characters (representing 1-12, 50, 100, 500, and 1000) |
RomanNumeral |
type |
Union type of RomanNumeralAscii and RomanNumeralUnicode |