Can't parseInt and toLocaleString At Same Time?


Sigh…another day, another mystery:

So apparently it’s not possible to insert commas into a number and have that number displayed as an integer as well??

Something like

var num = 1234567890.0887531;
document.write(num.toLocaleString('en'));   // displays "1,234,567,890.0887531" (without quotes)
document.write(parseInt(num));  // displays "1,234,567,890" (without quotes)

It seems .toLocaleString() never turns num into a string the way

var x = 45;
x = "45";

does…am I right about that? 'Cause that’s the whole premise behind my thinking, that .toLocaleString() turns something into a string, and not simply displays that something as a string for that one instance (which seems to be the case)…

Now, the really weird thing is this:

document.write(parseInt(num.toLocaleString('en')));   // displays "12" (without quotes), the first two digits

Why doesn’t that work as well??

Thanks – as usual!


what is “the right way”?

looking at the doc:

toLocaleString does a bit more then converting to string, it converts to a language (spoken language like Arabic) specific representation.

why would you want to do this?

num original is a float, then you cast num to string, then to integer? Why this order? To me, this seems illogical.



num is still a float, which you cast to integer

toLocaleString only wrote the result to the document, it didn’t affect num


I don’t know whether you’re suggesting my approach is wrong or just the syntax…I couldn’t figure out how to do this with the very basic JS I know right now so in desperation I googled and saw this “trick.” Works just fine for what I need – inserting commas into thousandth places in numbers – but doesn’t “play with” parseInt for some reason.

Yes I know; I’d seen it actually – interesting that some parts of the world use periods or even spaces to distinguish their thousands!

It’s part of a small exercise I came across – I mean, I already solved the simple problem and, of course, had to create a bigger one for myself!!

The output was simply a number but then I thought, hmm, hate how it’s so hard to read, can’t believe it’s 2018 A.D. and I still need to squint to see where the thousands are, exactly…so I thought to try my hand at inserting commas, feeling like I might know enough to do so!

Turned out it’s a thing that could be complicated for even knowledgeable programmers! Amazing, in this day and age, when numbers we routinely work with run into the millions and billions, there isn’t a native way in JS to easily display them (I mean easy enough for a beginner, which is really the best definition of “easy” there can be!)…

String conversion is because we can’t insert into number types, right, only strings – right? That’s my understanding, that we can’t just insert into a number but we can insert into strings.

Yes, but why’s it still a float…haven’t I “converted” it by now in the code? I mean, no, obviously, but why not?

Yes, so then you’re confirming

my thinking, that .toLocaleString() turns something into a string…

is incorrect, and that it

simply displays that something as a string for that one instance



i am not saying your approach is wrong or anything, just want to see what you think the right way is. Trying to better understand where and why you struggle

because toLocaleString returns the result, of course then we could store this result in a variable:

var num = 1234567890.0887531;

var x = num.toLocaleString('en')

now x will contain a string version of num. of course we could also store the returned result in num, overwriting/updating the variable

it does, you just didn’t update the variable. There is a difference there, i hope you can see that now more clearly with my example :slight_smile:

for me, the more logic order would be: float -> integer -> string



That works!

That was my whole mistake…yes, it makes so much sense now…oh thank you thank you thank you!!

I’ll sacrifice a few young broccoli to you…hallelujah!!


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