woocommerce/plugins/woocommerce-admin/packages/number
Kelly Dwan 754c2a07eb Update changelogs with latest versions, bump package versions where outdated (https://github.com/woocommerce/woocommerce-admin/pull/2039) 2019-04-11 14:43:45 -04:00
..
src Build: Create new number package (https://github.com/woocommerce/woocommerce-admin/pull/1399) 2019-01-28 15:34:56 -08:00
.npmrc Build: Create new number package (https://github.com/woocommerce/woocommerce-admin/pull/1399) 2019-01-28 15:34:56 -08:00
CHANGELOG.md Update changelogs with latest versions, bump package versions where outdated (https://github.com/woocommerce/woocommerce-admin/pull/2039) 2019-04-11 14:43:45 -04:00
README.md Build: Create new number package (https://github.com/woocommerce/woocommerce-admin/pull/1399) 2019-01-28 15:34:56 -08:00
package.json Update changelogs with latest versions, bump package versions where outdated (https://github.com/woocommerce/woocommerce-admin/pull/2039) 2019-04-11 14:43:45 -04:00

README.md

Number

A collection of utilities to propery localize numerical values in WooCommerce

Installation

Install the module

npm install @woocommerce/number --save

This package assumes that your code will run in an ES2015+ environment. If you're using an environment that has limited or no support for ES2015+ such as lower versions of IE then using core-js or @babel/polyfill will add support for these methods. Learn more about it in Babel docs.

Usage

import { formatNumber, formatValue, calculateDelta } from '@woocommerce/number';

// Formats a number using site's current locale.
// Defaults to en-US localization
const localizedNumber = formatNumber( 1337 ); // '1,377'

// formatValue's first argument is a type: average, or number
// The second argument is the number/value to format
const formattedAverage = formatValue( 'average', '10.5' ); // 11 just uses Math.round
const formattedNumber = formatValue( 'number', '1337' ); // 1,337 calls formatNumber ( see above )

// Get a rounded percent change/delta between two numbers
const delta = calculateDelta( 10, 8 ); // '25'