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?
By default, JavaScript displays numbers as base 10 decimals. But you can use the toString() method to output numbers from base 2 to base 36. Hexadecimal is base 16. Decimal is base 10. Octal is base 8.
This symbol can be a period ("."), as is common in United States and other English-speaking countries, or a comma (","), as in continental Europe. Decimal point and decimal comma are also common names for the decimal separator.
Use the toFixed() method to format a number to 2 decimal places, e.g. num. toFixed(2) . The toFixed method takes a parameter, representing how many digits should appear after the decimal and returns the result.
According to the specification, a DecimalLiteral is defined as:
DecimalLiteral ::
DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt
. DecimalDigits ExponentPartopt
DecimalIntegerLiteral ExponentPartopt
and for satisfying the parseFloat argument:
So numberString becomes the longest prefix of trimmedString that satisfies the syntax of a StrDecimalLiteral, meaning the first parseable literal string number it finds in the input. Only the .
can be used to specify a floating-point number. If you're accepting inputs from different locales, use a string replace:
function parseLocalNum(num) {
return +(num.replace(",", "."));
}
The function uses the unary operator instead of parseFloat because it seems to me that you want to be strict about the input. parseFloat("1ABC")
would be 1
, whereas using the unary operator +"1ABC"
returns NaN
. This makes it MUCH easier to validate the input. Using parseFloat is just guessing that the input is in the correct format.
use:
theNumber.toLocaleString();
to get a properly formatted string with the right decimal and thousands separators
As far as I'm aware, javascript itself only knows about the .
separator for decimals. At least one person whose judgement I trust on JS things concurs:
http://www.merlyn.demon.co.uk/js-maths.htm#DTS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With