Convenient Multi-language Number Formatting Library – numbro

Category: Javascript , Recommended | September 5, 2023
Author:BenjaminVanRyseghem
Views Total:7 views
Official Page:Go to website
Last Update:September 5, 2023
License:MIT

Preview:

Convenient Multi-language Number Formatting Library – numbro

Description:

numbro is a robust number formatting JavaScript library used for converting, formatting, and manipulating numbers with support for currency, time, percentage, and much more.

Basic usage:

Install and import the numbro module into your project.

# NPM
$ npm install numbro --save
# Bower
$ bower install numbro --save
// ES 6
import numbro from 'numbro';
// CommonJS:
const numbro = require('numbro');

Or include the JavaScript file directly on the webpage.

<script src="/dist/numbro.min.js"></script>

The JavaScript to format numbers you provide.

var string = numbro(10001).format({
    thousandSeparated: true,
    mantissa: 4
});
=> 10,001.0000
var string = numbro(10000.12).format({
    thousandSeparated: true,
    forceSign: true
});
=> +10,000.12
var string = numbro(1.23456).format({
    trimMantissa: true,
    mantissa: 4
});
=> 1.2346
var string = numbro(-10000).format({
    thousandSeparated: true,
    negative: "parenthesis",
    mantissa: 4
});
=> (10,000.0000)
var string = numbro(1234567).format({
    average: true,
    mantissa: 1
});
=> 1.2m
var string = numbro(-123456).format({
    spaceSeparated: false,
    average: true
});
=> -103k
var string = numbro(100).format({
    output: "ordinal"
});
=> 100th
var string = numbro(1234567891).format({
    average: true,
    totalLength: 3
});
=> 1.23b

Format currencies:

var string = numbro(1000.123).formatCurrency({
    thousandSeparated: true,
    mantissa: 2
});
=> 1,000.12
var string = numbro(1000.1).formatCurrency({
    average: true,
    mantissa: 2,
    optionalMantissa: true,
    currencyPosition: "postfix",
    spaceSeparated: true
});
=> 1k
var string = numbro(-1000.123).formatCurrency({
    thousandSeparated: true,
    negative: "parenthesis"
});
=> (1,000.123)

Format bytes:

var string = numbro(20).format({
    output: "byte",
    base: "binary"
});
=> 20B
var string = numbro(1024).format({
    output: "byte",
    base: "binary",
    spaceSeparated: true
});
=> 1 KiB
var string = numbro(1024).format({
    output: "byte",
    base: "binary",
    mantissa: 3,
    spaceSeparated: true
});
=> 1.024 KiB

Format percentages:

var string = numbro(0.5).format({
    output: "percent"
});
=> 50%
var string = numbro(0.12345).format({
    output: "percent",
    mantissa: 2
});
=> 12.34%
var string = numbro(-0.52).format({
    output: "percent",
    spaceSeparated: true
});
=> -52 %
var string = numbro(0.52).format({
    output: "percent",
    mantissa: 3,
    negative: "parenthesis",
    spaceSeparated: true
});
=> 52.000 %

Format a DateTime string.

var string = numbro(63846).format({
    output: "time"
});
=> 17:44:06

Unformat a string.

var string = numbro.unformat('($10,000.00)');

Manipulate the numbers with the following API.

var number = numbro(10000);
// 10010
var added = number.add(10);
// 9900
var added = number.subtract(100)
// 100000
var added = number.multiply(10)
// 1000
var added = number.divide(10)
// sets a new value
number.set(1000);
// finds the differences
value = 100;
var difference = number.difference(value);
// clones bumbers
var a = numbro(1000);
var b = numbro(a);
var c = a.clone();
var aVal = a.set(2000).value();
// 2000
var bVal = b.value();
// 1000
var cVal = c.add(10).value();
// 1010

Changelog:

v2.4.0 (09/05/2023)

  • Updated dependencies

v2.3.6 (09/03/2023)

  • Bugfixes

v2.3.5 (08/09/2021)

  • Bugfixes

v2.3.3 (08/08/2021)

  • Bugfixes

v2.3.2 (10/30/2020)

  • Bugfixes
  • Allow trimMantissa hint in string format.

v2.3.1 (06/18/2020)

  • Introduce lowPrecision to tweak precision when computing average value
  • Fix: spaceSeparated is not working for bytes

v2.3.0 (05/30/2020)

  • Lots of bugs fixed

v2.2.0 (03/18/2020)

  • Lots of bugs fixed

v2.1.2 (01/19/2019)

  • Fix: Update dependencies
  • Fix: Small changes to doc comments
  • Fix: Fixed unformat for non standard delimiters

v2.1.1 (10/18/2018)

  • Truncated numbers with trimMantissa and zero mantissa.
  • Fixes duplicate call of chooseLanguage when setting a language without subtag.
  • Enable es6 imports when using typescript.

07/14/2018

  • Fixes duplicate call of chooseLanguage when setting a language without subtag
  • Fixes duplicate call of chooseLanguage when setting a language without subtag
  • Fixes duplicate call of chooseLanguage
  • Add unit test for setLanguage

06/14/2018

  • Release 2.1.0

You Might Be Interested In:


Leave a Reply