JavaScript parseFloat in Different Cultures

JavascriptCulture

Javascript Problem Overview


I have a question about the default behavior of JavaScript's parseFloat function in different parts of the world.

In the US, if you call parseFloat on a string "123.34", you'd get a floating point number 123.34.

If I'm developing code in say Sweden or Brazil and they use a comma instead of a period as the decimal separator, does the parseFloat function expect "123,34" or "123.34".

Please note that I'm not asking how to parse a different culture's number format in the US. I'm asking does parseFloat in Sweden or Brazil behave the same way it does inside the US, or does it expect a number in its local format? Or to better think about this, does a developer in Brazil/Sweden have to convert strings to English format before it can use parseFloat after extracting text from a text box?

Please let me know if this doesn't make sense.

Javascript Solutions


Solution 1 - Javascript

parseFloat doesn't use your locale's definition, but the definition of a decimal literal.

It only parses . not ,

I'm brazilian and I have to replace comma with dot before parsing decimal numbers.

parseFloat specification

Solution 2 - Javascript

No, parseFloat is specified to parse DecimalLiterals, which use the dot as decimal separator. It does not depend on the current environment's locale settings.

Solution 3 - Javascript

It’s not just Sweden/Brazil. F.ex in US they often add commas in large numbers, like $5,762,325.25.

The parseFloat function essentially deals with decimals, not locale strings.

In general, JavaScript can sometimes convert generic strings/numbers/dates to locale-friendly formats, but not the other way around.

Solution 4 - Javascript

If you are sure it is in Brasilian format, just convert the number to US format before parsing.

function parseItalianNumber(stringNum) {
    return parseFloat(stringNum.replace(".","").replace(",","."));
}

in Italy we also use . as a thousands separator. This removes any thousands separators, just in case (you do not want many dots around), and then converts the comma to a dot, before calling parseFloat.

Solution 5 - Javascript

Complementing the answer given by FrancescoMM, you must use regex for really big numbers. String.replace using string as a parameter will replace only the first occurrence. So 999.999.999,99 becomes 999999.999.99

stringNum.replace(/\./g, "").replace(/\,/g, ".")

Source:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchumphriesView Question on Stackoverflow
Solution 1 - JavascriptAlcides QueirozView Answer on Stackoverflow
Solution 2 - JavascriptBergiView Answer on Stackoverflow
Solution 3 - JavascriptDavid HellsingView Answer on Stackoverflow
Solution 4 - JavascriptFrancescoMMView Answer on Stackoverflow
Solution 5 - JavascriptJoão SaidlerView Answer on Stackoverflow