A thought struck me as I was writing a piece of JavaScript code that processed some floating point values. What is the decimal point symbol in JavaScript? Is it always .
? Or is it culture-specific? And what about .toFixed()
and .parseFloat()
? If I'm processing a user input, it's likely to include the local culture-specific decimal separator symbol.
Ultimately I'd like to write code that supports both decimal points in user input - culture-specific and .
, but I can't write such a code if I don't know what JavaScript expects.
Added: OK, Rubens Farias suggests to look at similar question which has a neat accepted answer:
function whatDecimalSeparator() {
var n = 1.1;
n = n.toLocaleString().substring(1, 2);
return n;
}
That's nice, it lets me get the locale decimal point. A step towards the solution, no doubt.
Now, the remaining part would be to determine what the behavior of .parseFloat()
is. Several answers point out that for floating point literals only .
is valid. Does .parseFloat()
act the same way? Or might it require the local decimal separator in some browser? Are there any different methods for parsing floating point numbers as well? Should I roll out my own just-to-be-sure?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…