Formatting a number with commas as a thousand digits without converting it into a string

I’ve tried this but when I checked it with console.log(isNaN(conversion));, it returns true. How do you format this without it converting into a string?

const income = Number(8000);
const conversion = income.toLocaleString(navigator.language, {
  minimumFractionDigits: 2


codesandbox: Formatting a whole number with thousands - CodeSandbox

1 Like

You cant really. Numbers does not support commas

1 Like

In ES2021 (I think?) there is support for using an underscore as a separator, like:

const c = 299_792_458

but that is only in code for number literals, for readability for programmers - it will not affect how it shows on the screen. As @adramelech says, any number formatting must be done as a string. It is pretty easy to write a function for that (or find one online) or there are libraries that handle number formatting.

1 Like

I don’t see what the need is. Assuming you have income in scope, then you’ve got your number. You want to format it, but also continue to perform operations on it? Formatting should be the last step before tossing the number into the UI.


Yeah, I agree with that 100%. Over the years I’ve created a lot of hassles by formatting numbers, date/times, etc. too early. Format as a last step before the UI. Until then, keep numbers and numbers and date/times as UTC time stamps.

Thank you. I just noticed that I could still save it as a number, I only have to show the comma on the UI level.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.