← Back to JavaScript, page 9

javascript-0162 · Easy fresher

What is the difference between parseInt, parseFloat, and Number, and how do you round and format numbers for display?

Number(text) converts the whole string and gives NaN if any part is invalid, while parseInt and parseFloat read as many leading characters as they can and ignore the rest. Always pass a radix to parseInt. toFixed returns a string and rounds based on the binary value, so display money with Intl.NumberFormat or integer cents, and compare floating point results with a tolerance.

Full answer

Converting text to a number:

  • Number('42') is 42, Number('') is 0, Number(' 12 ') is 12, Number('12px') is NaN, and Number('0x1f') is 31. It is strict: the whole string must be a number.
  • parseInt('12px', 10) is 12. It reads digits from the start and stops at the first invalid character. parseInt('') is NaN. Always pass the radix as the second argument: parseInt('08', 10). parseInt(0.0000005) is 5 because the number is converted to the string '5e-7' first, so do not use it to truncate numbers. Use Math.trunc.
  • parseFloat('3.14abc') is 3.14, and it understands exponent notation. It has no radix.
  • Unary plus, +'42', behaves like Number.
  • Number.isNaN is the right way to check the result.

Rounding: Math.round, Math.floor, Math.ceil, and Math.trunc work on integers. For decimals, num.toFixed(2) returns a string, not a number, and its rounding follows the binary value: (1.005).toFixed(2) is '1.00', because 1.005 is stored slightly below 1.005. To round to decimals as a number, use Math.round(x * 100) / 100 with care, or Number(x.toFixed(2)). For money, keep integers (cents) or use a decimal library, and format only for display.

Formatting for people: num.toLocaleString('en-US') and Intl.NumberFormat add grouping, currency, percent, and compact notation, and follow local conventions (1.234,5 in German). Create one formatter and reuse it for lists.

Other checks: Number.isInteger, Number.isFinite (unlike the global isFinite, no conversion), Number.MAX_SAFE_INTEGER for exact integers, and toString(2) or parseInt(text, 2) for other bases. Floating point effects such as 0.1 + 0.2 !== 0.3 come from binary storage; compare with a small tolerance, Math.abs(a - b) < Number.EPSILON, for values near 1.

javascript
for (const text of ['42', '', ' 12 ', '12px', '3.14abc', '0x1f', '1e3', '-7', 'abc', '1,000']) {
  console.log(JSON.stringify(text).padEnd(10), 'Number:', String(Number(text)).padEnd(5), 'parseInt:', String(parseInt(text, 10)).padEnd(5), 'parseFloat:', parseFloat(text));
}
console.log(parseInt('08', 10), parseInt('ff', 16), parseInt('101', 2), (255).toString(16), (5).toString(2));
console.log('parseInt on a tiny number:', parseInt(0.0000005), '| Math.trunc:', Math.trunc(0.0000005));
console.log(['1', '2', '3'].map(Number), ['1', '2', '3'].map(parseInt));

console.log(Math.round(2.5), Math.round(-2.5), Math.round(2.4), Math.floor(-2.5), Math.ceil(-2.5), Math.trunc(-2.5));
console.log((1.005).toFixed(2), (1.255).toFixed(2), (2.5).toFixed(0), typeof (3).toFixed(2));
console.log(Math.round(1.005 * 100) / 100, Math.round((1.005 + Number.EPSILON) * 100) / 100);
console.log(0.1 + 0.2 === 0.3, Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON);

const cents = 1999;
console.log('money as integer cents:', cents / 100, new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(cents / 100));
console.log((1234567.891).toLocaleString('en-US'), (1234567.891).toLocaleString('de-DE'), (1234567.891).toLocaleString('en-IN'));
console.log(new Intl.NumberFormat('en-US', { style: 'percent', maximumFractionDigits: 1 }).format(0.4567));
console.log(new Intl.NumberFormat('en', { notation: 'compact' }).format(1530000), new Intl.NumberFormat('en-US', { minimumFractionDigits: 2 }).format(5));

console.log(Number.isInteger(5.0), Number.isFinite('5'), isFinite('5'), Number.isNaN('abc'), isNaN('abc'));

Run with Node.js v24.22.1 as js-162.js; two runs gave identical output. Output: "42" Number: 42 parseInt: 42 parseFloat: 42 "" Number: 0 parseInt: NaN parseFloat: NaN " 12 " Number: 12 parseInt: 12 parseFloat: 12 "12px" Number: NaN parseInt: 12 parseFloat: 12 "3.14abc" Number: NaN parseInt: 3 parseFloat: 3.14 "0x1f" Number: 31 parseInt: 0 parseFloat: 0 "1e3" Number: 1000 parseInt: 1 parseFloat: 1000 "-7" Number: -7 parseInt: -7 parseFloat: -7 "abc" Number: NaN parseInt: NaN parseFloat: NaN "1,000" Number: NaN parseInt: 1 parseFloat: 1 8 255 5 ff 101 parseInt on a tiny number: 5 | Math.trunc: 0 [ 1, 2, 3 ] [ 1, NaN, NaN ] 3 -2 2 -3 -2 -2 1.00 1.25 3 string 1 1.01 false true money as integer cents: 19.99 $19.99 1,234,567.891 1.234.567,891 12,34,567.891 45.7% 1.5M 5.00 true false true false true

Key points

  • Number is strict and whole-string; parseInt and parseFloat read a valid prefix.
  • Always give parseInt a radix; do not use it to truncate numbers.
  • toFixed returns a string and can round unexpectedly; use cents or a decimal library for money.
  • Format for people with Intl.NumberFormat or toLocaleString.
  • Compare floats with a tolerance and check results with Number.isNaN.

Common mistakes

  • Calling parseInt without a radix, or passing it directly to map.
  • Using toFixed for calculations and forgetting that it returns a string.
  • Storing money as floating point numbers.
  • Using the global isNaN and isFinite, which convert their argument first.

Follow-ups

  • Why does (1.005).toFixed(2) give 1.00?
  • How would you round to the nearest 0.05?
  • How do you format a number with a currency symbol for different countries?

Tips

  • Contrast strict Number with the prefix-reading parse functions.
  • Give the money rule: integers or a decimal library, format for display only.
  • Mention Intl.NumberFormat for locales.