Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

format-quantity

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

format-quantity

[![npm version](https://badge.fury.io/js/format-quantity.svg)](//npmjs.com/package/format-quantity) ![workflow status](https://github.com/jakeboone02/format-quantity/workflows/Continuous%20Integration/badge.svg) [![codecov.io](https://codecov.io/github/ja

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
774
increased by9.94%
Maintainers
1
Weekly downloads
 
Created
Source

format-quantity

npm version workflow status 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
npm i format-quantity

# yarn
yarn add format-quantity

Browser

In the browser, available as a global function formatQuantity.

<script src="https://unpkg.com/format-quantity"></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).

Keywords

FAQs

Package last updated on 11 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc