Press "Enter" to skip to content

Javascript Multilingual Number Format Library | numbro

numbro is a javascript library for formatting and manipulating numbers. numbro is a powerful JavaScript number format library used to convert, format, and manipulate numbers with support for the currency, time, percentage, and more.

html format number thousands separator, input typenumber comma separator, javascript number formatting, numbro format, autonumeric, convert 1000 to 1k javascript

How to make use of it:

Install and import the numbro module into your undertaking.

# NPM
$ npm install numbro --save

# Bower
$ bower install numbro --save
// ES 6
import numbro from 'numbro';

// CommonJS:
const numbro = require('numbro');

Or embody the JavaScript file immediately 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 next 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

Convenient Multi-language Number Formatting Library, numbro Plugin/Github, numbro npm, auto format number input


See Demo And Download

Official Website(BenjaminVanRyseghem): Click Here

This superior jQuery/javascript plugin is developed by BenjaminVanRyseghem. For extra advanced usage, please go to the official website.

Be First to Comment

    Leave a Reply

    Your email address will not be published.