JavaScript: Format Number

By Xah Lee. Date: . Last updated: .

JavaScript does not have a “format” or “printf” function. The following code are solutions.

Truncate Decimal

number.toFixed(n) → returns a string representation of number, with n digits after decimal, padded by 0 if necessary.

// truncate number to integer
console.log(
    3.12345.toFixed(0) // 3
);

// truncate number, leave just 1 decimal place
console.log(
    3.12345.toFixed(1) // 3.1
);

// truncate number. leave just 3 decimal place, pad with 0 if necessary
console.log(
    (3.1).toFixed(3) // 3.100
);

// the arg to toFixed cannot be negative

Manipulating Number Format with Regex

Sometimes you'll need to manipulate number format in string. Here's some examples.

// number to string, then truncate, by regex
// leave 1 decimal place
console.log(
    (3.123).toString().replace( /\.(\d)\d+/,".$1")
); // "3.1"

// number to string
// leave at most 2 decimal places. no padding
function formatNumber (n) { return (n).toString().replace( /\.(\d\d)\d+/,".$1"); }

console.log(
    formatNumber(3)
); // "3"

console.log(
    formatNumber(3.1)
); // "3.1"

console.log(
    formatNumber(3.12)
); // "3.12"

console.log(
    formatNumber(3.123)
); // "3.12"

Format Number with Metric Prefix

function formatNumber (n,m) {
    // format number with metric prefix such as 1.2k
    // n is integer. The number to be converted
    // m is integer. The number of decimal places to show. Default to 1.
    // returns a string, with possibly one of k M G T ... suffix. Show 1 decimal digit

    // http://xahlee.info/js/javascript_format_number.html
    // version 2016-04-20

    var prefix = ["", " k", " M", " G", " T", " P", " E", " Z", " Y", " * 10^27", " * 10^30", " * 10^33"]; // should be enough. Number.MAX_VALUE is about 10^308
    var ii = 0;
    var m = ( m === undefined ? 1 : m );
    while ((n = n/1000) >= 1) { ii++; }
    return (n * 1000).toFixed(m).toString() + prefix[ii];
};

console.log(
    formatNumber(111, 1) === "111.0"
); // true

console.log(
    formatNumber(111222, 1) === "111.2 k"
); // true

console.log(
    formatNumber(111222333, 1) === "111.2 M"
); // true

console.log(
    formatNumber(111222333444, 1) === "111.2 G"
); // true

console.log(
    formatNumber(111222333444) === "111.2 G"
); // true

console.log(
    formatNumber(111222333444, 3) === "111.222 G"
); // true

console.log(
    formatNumber(111222333444, 0) === "111 G"
); // true

String Topic

  1. JavaScript: Default Charset/Encoding
  2. JavaScript: String is 16-Bit Unit Sequence
  3. JavaScript: Unicode Character Escape Sequence
  4. JavaScript: Allowed Characters in Identifier
  5. HTML: Allowed Characters in id Attribute
  6. HTML: Character Sets and Encoding
  7. HTML XML Entities

  1. JavaScript: Template String
  2. JavaScript: Convert String to Number
  3. JavaScript Encode URL, Escape String
  4. JavaScript: Format Number
  5. JavaScript: JSON Object

  1. JavaScript: String Object
  2. JavaScript: String.prototype
  3. JavaScript: String Constructor

RegExp Topic

  1. JavaScript: RegExp Tutorial
  2. JavaScript: String Methods for RegExp
  3. JavaScript: RegExp Object
  4. JavaScript: RegExp Constructor
  5. JavaScript: RegExp.prototype
  6. JavaScript: RegExp Syntax
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.