So apparently it’s not possible to insert commas into a number and have that number displayed as an integer as well??
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
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