Skip to content
Format a number as an integer plus fraction, as seen in recipe ingredient lists
TypeScript JavaScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitignore
.travis.yml
CHANGELOG.md
LICENSE
README.md
_config.yml
babel.config.js
package.json
rollup.config.js
tsconfig.json
yarn.lock

README.md

format-quantity

npm version Travis (.org) codecov.io downloads MIT License

Formats a number (or string that appears to be a number) as one would see it written in imperial measurements, e.g. "1 1/2" instead of "1.5". To use unicode vulgar fractions like "⅞", pass true as the second argument.

For the inverse operation, converting a string (which may include mixed numbers or vulgar fractions) to a number, check out numeric-quantity.

Installation

npm

npm i format-quantity

or

yarn add format-quantity

Browser

In the browser, available as a global function formatQuantity.

<script src="path/to/format-quantity.umd.js"></script>
<script>
  console.log(formatQuantity(10.5)); // "10 1/2"
</script>

Usage

import formatQuantity from 'format-quantity';

console.log(formatQuantity(1.5)); // "1 1/2"
console.log(formatQuantity(2.66)); // "2 2/3"
console.log(formatQuantity(3.875, true)); // "3⅞"

The return value will be null if the provided argument is not a number or a string that evaluates to a number using parseFloat. The return value will be an empty string ("") if the provided argument is 0 or "0" (this is done to fit the primary use case of recipe ingredients).

You can’t perform that action at this time.