不同文化中的JavaScript parseFloat [英] JavaScript parseFloat in Different Cultures

查看:171
本文介绍了不同文化中的JavaScript parseFloat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于JavaScript的parseFloat函数在世界不同地方的默认行为的问题。

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

在美国,如果你在一个字符串123.34上调用parseFloat,你会得到一个浮点数123.34。

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

如果我在瑞典或巴西开发代码并且他们使用逗号代替句点作为小数分隔符,那么parseFloat函数是否期望123,34或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".

请注意,我不是在问美国如何解析不同文化的数字格式。我问的是瑞典或巴西的parseFloat在美国的表现方式是否相同,或者它是否期望以本地格式显示数字?或者为了更好地考虑这一点,巴西/瑞典的开发人员是否必须先将字符串转换为英文格式,然后才能在从文本框中提取文本后使用parseFloat?

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.

推荐答案

parseFloat 不使用您的语言环境的定义,而是使用十进制文字的定义。

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

它只解析不是

我是巴西人,在解析十进制数字之前我必须用点替换逗号。

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

parseFloat规范

这篇关于不同文化中的JavaScript parseFloat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆