https://github.com/jakeboone02/numeric-quantity
Convert integer plus fraction into a decimal value
https://github.com/jakeboone02/numeric-quantity
converts fraction javascript quantity
Last synced: 6 months ago
JSON representation
Convert integer plus fraction into a decimal value
- Host: GitHub
- URL: https://github.com/jakeboone02/numeric-quantity
- Owner: jakeboone02
- License: mit
- Created: 2015-03-18T15:40:38.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2025-04-01T00:34:48.000Z (6 months ago)
- Last Synced: 2025-04-08T22:08:13.541Z (6 months ago)
- Topics: converts, fraction, javascript, quantity
- Language: TypeScript
- Homepage: https://jakeboone02.github.io/numeric-quantity/
- Size: 3.48 MB
- Stars: 22
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[![npm][badge-npm]](https://www.npmjs.com/package/numeric-quantity)

[](https://codecov.io/github/jakeboone02/numeric-quantity?branch=main)
[](http://npm-stat.com/charts.html?package=numeric-quantity&from=2015-08-01)
[](http://opensource.org/licenses/MIT)Converts a string to a number, like an enhanced version of `parseFloat`.
**[Full documentation](https://jakeboone02.github.io/numeric-quantity/)**
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.
- 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](https://www.npmjs.com/package/format-quantity)._
>
> _For a more complete solution to parsing recipe ingredients, try [parse-ingredient](https://www.npmjs.com/package/parse-ingredient)._## Usage
### Installed
```js
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:
```html
import { numericQuantity } from 'https://cdn.jsdelivr.net/npm/numeric-quantity/+esm';
console.log(numericQuantity('10½')); // 10.5
```
As UMD (all exports are properties of the global object `NumericQuantity`):
```html
console.log(NumericQuantity.numericQuantity('xii', { romanNumerals: true })); // 12
```
[badge-npm]: https://img.shields.io/npm/v/numeric-quantity.svg?cacheSeconds=3600&logo=npm