TExcelTAD
Home › JavaScript › JS Number Methods

JavaScript Number Methods

Numbers have methods for formatting them as strings, and the global Number object provides functions for parsing strings into numbers and checking what kind of number you have.

Formatting: toFixed, toPrecision, toString

js · try it yourself
const pi = 3.14159265;
console.log(pi.toFixed(2));        // 2 digits after the point
console.log(pi.toFixed(0));
console.log((2.5).toFixed(2));     // pads with zeros
console.log(pi.toPrecision(4));    // 4 significant digits
console.log((1234.5).toPrecision(2));
console.log((255).toString(16), (255).toString(2));
3.14
3
2.50
3.142
1.2e+3
ff 11111111

toFixed and toPrecision return strings. That's ideal for display, but convert back to a number before doing further maths.

Note: 5.toFixed(2) is a syntax error — the parser thinks the dot is a decimal point. Write (5).toFixed(2) or use a variable.

Converting strings to numbers

Function Behaviour "42px" "3.9" "" "abc"
Number(s) whole string must be numeric NaN 3.9 0 NaN
parseInt(s, 10) reads leading digits, integer only 42 3 NaN NaN
parseFloat(s) reads a leading decimal number 42 3.9 NaN NaN
+s (unary plus) same as Number(s) NaN 3.9 0 NaN
js · try it yourself
console.log(Number("42"), Number(" 42 "), Number("42px"));
console.log(parseInt("42px", 10), parseFloat("3.9kg"));
console.log(parseInt("ff", 16), parseInt("1010", 2));
console.log(Number(true), Number(null), Number(undefined));
console.log(+"7.5" + 1);
42 42 NaN
42 3.9
255 10
1 0 NaN
8.5

Tip: Always pass the radix (10) to parseInt. Use Number() when the whole input must be valid, and parseInt/parseFloat when you want to read a number at the start of text like "12px".

Checking numbers

js · try it yourself
console.log(Number.isInteger(5), Number.isInteger(5.5));
console.log(Number.isSafeInteger(2 ** 60));
console.log(Number.isFinite(100), Number.isFinite("100"));
console.log(Number.isNaN(NaN), Number.isNaN("hello"));
console.log(isNaN("hello"));   // global version converts first — confusing
true false
false
true false
true false
true

The Number.* versions never convert their argument, so Number.isFinite("100") is false because a string is not a number. The older global isNaN and isFinite convert first, which leads to surprises.

Number constants

Property Value
Number.MAX_SAFE_INTEGER 9007199254740991
Number.MIN_SAFE_INTEGER -9007199254740991
Number.MAX_VALUE ≈ 1.8e308
Number.MIN_VALUE ≈ 5e-324 (smallest positive)
Number.EPSILON ≈ 2.2e-16 (gap between 1 and the next number)
Number.POSITIVE_INFINITY Infinity
Number.NaN NaN

Locale formatting

toLocaleString and Intl.NumberFormat format numbers the way people in a given region expect — thousands separators, currency symbols and so on. Pass an explicit locale so the result doesn't depend on the user's settings:

js · try it yourself
const amount = 1234567.891;
console.log(amount.toLocaleString("en-US"));
console.log(amount.toLocaleString("de-DE"));
console.log(
  new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" }).format(amount)
);
console.log(
  new Intl.NumberFormat("en-US", { style: "percent" }).format(0.256)
);
1,234,567.891
1.234.567,891
$1,234,567.89
26%

A practical example

Parsing user input safely and formatting the result:

js · try it yourself
function parsePrice(text) {
  const n = Number.parseFloat(text.replace(",", "."));
  return Number.isFinite(n) ? n : null;
}

for (const input of ["19.99", "7,5", "free", "  3 "]) {
  const price = parsePrice(input);
  console.log(JSON.stringify(input), "->", price === null ? "invalid" : price.toFixed(2));
}
"19.99" -> 19.99
"7,5" -> 7.50
"free" -> invalid
"  3 " -> 3.00

Test yourself

What does (9.876).toFixed(1) return?

  1. 9.8
  2. "9.9"
  3. 9.9
  4. "9.8"

What is parseInt("12.7cm", 10)?

  1. 12.7
  2. 13
  3. 12
  4. NaN

What is Number("12.7cm")?

  1. 12.7
  2. 12
  3. NaN
  4. 0