numerable is a number formatting library for Javascript and Typescript apps.
Use npm or yarn to install numerable.
npm install --save numerable
# or
yarn add numerable
import { format, parse } from 'numerable';
format(1500250.2, '0,0.00');
//=> '1,500,250.20'
format(0.25, '0.0 %');
//=> '25.0 %'
format(1200, '0.00 a');
//=> '1.20 K'
parse('80.5%');
//=> 0.805
Numbers formatting in numerable is done through a pattern-based syntax. With these patterns you can easily define common number formats, similar to date formatting.
The numeric pattern syntax defines:
- Amount of decimal places
- Thousands separator (grouping)
- Number sign type (+ - and ())
- Number sign position
- Positive sign visibility
Examples
By adding the % symbol to any of the previous patterns, the value is multiplied by 100 and the % symbol is added in the place indicated.
Number | Pattern | Result | Locale |
---|---|---|---|
0.52 | "0.##%" | "52%" | en (English) |
1 | "0.##%" | "100%" | en (English) |
1 | "0,0.00Β %" | "100.00Β %" | en (English) |
-0.88 | "0Β %" | "-88Β %" | en (English) |
If an abbreviation is specified in the pattern (a), numerable will look for the shortest abbreviation for your number, and it will display the number with a locale-specific abbreviation.
numerable will format the currency symbol if the currency ISO code (ISO 4217) is passed as a format option (e.g. format(155, '$ 0.00', { currency: 'EUR' })
) and the dollar symbol ($) is found in the pattern.
numerable allows you to format bytes by adding the 'bd' or 'bb' characters to the pattern.
- 'bd' format bytes in a decimal scale (1000)
- 'bb' format bytes in a binary scale (1024)
numerable allows you to format ordinal numbers based on the locale. The character 'o' in the mask will enable the ordinal numbers formatting.
Given an amount of seconds, it will display hours, minutes, and seconds.
- Syntax
format(number, [pattern="0,0.##########"], [options=DEFAULT_OPTIONS])
-
number
- type: number | string | null | undefined
- The number to format
-
pattern
- type: string | null | undefined
- default: "0,0.##########"
- The pattern used to format the provided number.
- examples:
"0.00"
"0,0.00##"
"0,0.00 %"
"0.0 a"
-
options
- type: FormatOptions | null | undefined
- The options to apply in the format process.
- example:
format(1234.56, '0,0.00', { defaultPattern: '0,0.##########', nullFormat: 'N/A', nanFormat: 'NaN', zeroFormat: '-', rounding: 'floor', locale: fr, });
- FormatOptions properties:
-
defaultPattern:
- type: string
- default: "0,0.##########"
- It will be the formatting pattern applied in case that the provided pattern in the arguments is null, undefined or "" (empty string).
-
rounding:
- type:
- string: "truncate" | "ceil" | "floor" | "half-up" | "half-away-from-zero"
or - function: (value: number) => number
- string: "truncate" | "ceil" | "floor" | "half-up" | "half-away-from-zero"
- default: "half-away-from-zero"
- It will be the rounding function applied to the number in the resolved maximum amount of decimal places.
- type:
-
nullFormat:
- type: string
- default: ""
- If defined, it will be the returned string when the value is null | undefined | NaN.
-
nanFormat:
- type: string
- default: ""
- If defined, it will be the returned string when the value is NaN.
-
zeroFormat:
- type: string
- default: undefined
- If defined, it will be the returned string when the value is 0.
-
locale:
- type: NumerableLocale
- default: en (english NumerableLocale)
- If a NumerableLocale is provided, it will format the number with the specified locale configuration.
-
scalePercentage:
- type: boolean
- default: true
- If true (default), it will multiply the number by 100 when a percentage format is detected. If false, the number won't be scaled when a percentage format is detected.
-
trim:
- type: boolean
- default: true
- If true (default), it will trim the output formatted string. If false, the surrounding spaces will be preserved.
-
signedZero:
- type: boolean
- default: false
- If true, it will display a negative sign for numbers that before the rounding where negative, but after rounding became zero. If false (default), it will never append the minus sign (-) to zero.
- This option also applies to the positive zeros if the force sign option is defined on the pattern (+ sign in the pattern).
- Example:
format(-0.008, '0.0', { signedZero: true }) //=> '-0.0'
format(0.008, '+0.0', { signedZero: true }) //=> '+0.0'
-
nonBreakingSpace:
- type: boolean
- default: false
- If true, the spaces in the resulting string will be replaced with non-breaking-spaces. This will help preventing a line break when a normal space is found (although in the end, this will depend on the display engine).
-
- Syntax
parse(numberString, [options=DEFAULT_OPTIONS])
-
numberString
- type: string
- The numeric string to parse
-
options
- type: FormatOptions | null | undefined
- The options to apply in the parse process.
- Same options as in format() function.
numerable includes two functions that support i18n:
- format
- parse
In order to handle internationalization:
- Import a numerable locale and pass it to the locale option.
import { format, parse } from 'numerable'; import { es, enIN, fr, de } from 'numerable/locale'; format(2500000, '0,0.0 a', { locale: es }); //=> '2,5 M' format(2500000, '0,0.0 a', { locale: enIN }); //=> '25.0 L' format(2500000.25, '0,0.00', { locale: fr }); //=> '2 500 000,25' format(1.5, '0.00', { locale: de }); //=> '1,5' parse('1,5', { locale: de }); //=> 1.5
To make your app internationalization handling easy, you can create your own function wrappers and use those instead of the original functions. With this approach, you can make your app localized number formatting API simple.
// my-app/utils/format.js
β
import { format } from 'numerable';
import { es, fr, enIN } from 'numerable/locale';
β
const locales = {
es,
fr,
'en-in': enIN,
};
β
export default function (value, pattern = '0,0.000', options) {
const appLocaleId = window.__myLocaleId__;
return format(value, pattern, { locale: locales[appLocaleId], ...options });
}
β
// Later...
// Importing my custom localized format wrapper
import format from 'my-app/utils/format';
β
window.__myLocaleId__ = 'es';
format(1234.56, '0,0.00');
// => "1.234,56"
β
// With our wrapper function, if the pattern is omitted, it will take the default from the wrapper function.
window.__myLocaleId__ = 'fr';
format(1234.56);
// => "1 234,560"
β
window.__myLocaleId__ = 'en-in';
format(123456789.12, '0,0.0');
// => "12,34,56,789.1"
// 'en' will work, as it is the default locale of numerable
window.__myLocaleId__ = 'en';
format(1234.56, '0,0.00');
// => "1,234.56"
If a NumerableLocale is provided in any format function, it will format the number with the specified locale.
The locales provided by numerable allow localized support for the following formatting options:
- Thousands and decimal delimiters
- Abbreviations (e.g. "1.5 K")
- Digit grouping style (e.g. "10,00,000.00" or "100,0000.00")
- Different numeral systems (e.g. "Ω’βΩ¬Ω₯βΩ©βΩ©βΩ¬Ω¦βΩ₯βΩ€βΩ«Ω β")
- Ordinal numbers (e.g. "3rd")
import { format } from 'numerable';
import { fr, es, enIN, zh } from 'numerable/locale';
format(1234.56, '0.000', { locale: fr }); // Returns "1 234,560"
format(2500000, '0,0.0 a', { locale: es }); //=> '2,5 M'
format(2500000, '0,0.0 a', { locale: enIN }); //=> '25.0 L'
format(2500000, '0,0.0', { locale: zh }); //=> '250,0000.0'
format(2544609, '0,0.0', { locale: arEG }); //=> 'Ω’βΩ¬Ω₯βΩ€βΩ€βΩ¬Ω¦βΩ βΩ©βΩ«Ω β'
If you app is simple and you are not supporting legacy browsers or Node. You can create a NumerableLocale based on the platform.
β οΈ Take into account that the support for platform locales is limited. And some of the features, like "ordinal numbers formatting" won't be available if you obtain the locale from the platform.
numerable provides the function getLocaleFromPlatform. This function will dynamically generate a NumerableLocale using the platform Intl.NumberFormat configuration.
import { format, getLocaleFromPlatform } from 'numerable';
format(2500, '0,0.0 a', { locale: getLocaleFromPlatform('fr') }); //=> '2,5 k'
format(2500, '0,0.0 a', { locale: getLocaleFromPlatform('cs') }); //=> '2,5 tis.'
format(2500, '0.0 a', { locale: getLocaleFromPlatform('es') }); //=> '2,5 mil'
Cons:
- Some features won't work on legacy browsers like IE11 or Node (they will fallback to en language), resulting in potential inconsistency across browsers.
- No full support for formatting features like "ordinal number formatting"
numerable started as a fork from Adam Draper's project Numeral.js.
It has been completely rewriten in Typescript with a functional API and extended features.
Also, date-fns served as inspiration for some of the features.
The patterns used in numerable are an extended version of the original patterns created by Numeral.js.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.