Javascript解析浮点数忽略逗号后面的小数
这是一个简单的场景。我想显示我的网站上显示的两个值的减法:
//Value on my websites HTML is: "75,00"
var fullcost = parseFloat($("#fullcost").text());
//Value on my websites HTML is: "0,03"
var auctioncost = parseFloat($("#auctioncost").text());
alert(fullcost); //Outputs: 75
alert(auctioncost); //Ouputs: 0
谁能告诉我我做错了什么?
Here's a simple scenario. I want to show the subtraction of two values show on my site:
//Value on my websites HTML is: "75,00"
var fullcost = parseFloat($("#fullcost").text());
//Value on my websites HTML is: "0,03"
var auctioncost = parseFloat($("#auctioncost").text());
alert(fullcost); //Outputs: 75
alert(auctioncost); //Ouputs: 0
Can anyone tell me what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这是“按设计”。 parseFloat 函数只会考虑字符串中的部分,直到达到非 +、-、数字、指数或小数点。一旦它看到逗号,它就会停止查找并只考虑“75”部分。
要解决此问题,请将逗号转换为小数点。
This is "By Design". The
parseFloat
function will only consider the parts of the string up until in reaches a non +, -, number, exponent or decimal point. Once it sees the comma it stops looking and only considers the "75" portion.To fix this convert the commas to decimal points.
javascript 的 parseFloat 不接受区域设置参数。因此,您必须将
,
替换为.
javascript's parseFloat doesn't take a locale parameter. So you will have to replace
,
with.
为什么不使用全球化?这只是当您不使用英语时可能遇到的问题之一:
stackoverflow 上的一些链接可供查看:
Why not use globalize? This is only one of the issues that you can run in to when you don't use the english language:
Some links on stackoverflow to look into:
parseFloat
根据 JavaScript 进行解析 十进制文字的定义,而不是您所在区域的定义。 (例如,parseFloat
不支持区域设置。)JavaScript 中的十进制文字使用.
作为小数点。parseFloat
parses according to the JavaScript definition of a decimal literal, not your locale's definition. (E.g.,parseFloat
is not locale-aware.) Decimal literals in JavaScript use.
for the decimal point.正如 @JaredPar 在他的答案中指出的那样,使用
parseFloat
替换只需替换
逗号
与一个dot
会修复,除非它是一个超过数千的数字,例如1.000.000,00
这样会给你错误的数字。因此,您需要替换逗号
并删除点
。通过使用两次替换,您将能够处理数据而不会在输出中接收到错误的数字。
As @JaredPar pointed out in his answer use
parseFloat
with replaceJust replacing the
comma
with adot
will fix, Unless it's a number over the thousands like1.000.000,00
this way will give you the wrong digit. So you need to replace thecomma
remove thedots
.By using two replaces you'll be able to deal with the data without receiving wrong digits in the output.
对于到达这里的任何人,想知道如何处理可能涉及逗号(
,
)和句号(.
)但确切的数字格式可能不知道的问题 - 这这就是我在使用parseFloat()
之前更正字符串的方法(借鉴其他答案的想法):至少,这将允许解析英国/美国和欧洲十进制格式(假设字符串包含有效的数字)。
For anyone arriving here wondering how to deal with this problem where commas (
,
) and full stops (.
) might be involved but the exact number format may not be known - this is how I correct a string before usingparseFloat()
(borrowing ideas from other answers):At the very least, this would allow parsing of British/American and European decimal formats (assuming the string contains a valid number).
最好使用这种语法来替换一百万个情况下的所有逗号 1,234,567
g
表示删除所有逗号。在此处查看 Jsfiddle 演示。
It is better to use this syntax to replace all the commas in a case of a million 1,234,567
The
g
means to remove all commas.Check the Jsfiddle demo here.
JS 中的数字使用
.
(句号/句点)字符来指示小数点,而不是,
(逗号)。Numbers in JS use a
.
(full stop / period) character to indicate the decimal point not a,
(comma).你做错的地方是向
parseFloat()
提供以面向人类的表示法表示小数的字符串,而parseFloat()
仅接受与 JavaScript 数字文字与区域无关,始终使用点作为小数分隔符,并且没有千位分隔符。此外,在所有答案中使用的这个
parseFloat()
函数在接受正确输入时过于慷慨,从而阻止了对大多数情况下不正确输入的检测:为了获得更严格的 输入为了更好地控制行为,我建议您实现自己的解析函数。这是我的:
还有一个使用
parse_float()
来解析您的格式的数字的测试程序:它写道:
What you do wrong is feed
parseFloat()
with strings that represent decimal fractions in a human-oriented notation, whereasparseFloat()
accepts only the standard format corresponding with the JavaScript number literals, which are region-independent, always use the dot as the decimal separator, and have no thousands separator.Futhermore, this
parseFloat()
function, used in all the answers, is too generous in what it accepts as correct input, preventing the detection of what in most cases is incorrect input:In order to get a stricter and therefore better-controlled behavior, I recommend that you implement your own parsing function. Here is mine:
And a test program that uses
parse_float()
to parse numbers of your format:It writes:
就我而言,我已经有一个句号
(.)
和一个逗号(,)
,所以对我有用的是替换
逗号(,)
和一个空字符串,如下所示:假设现有数据库中的金额为 3,000.78。结果是:
3000.78
,没有起始逗号(,)
。In my case, I already had a period
(.)
and also a comma(,)
, so what worked for me was toreplace
the comma(,)
with an empty string like below:This is assuming that the amount from the existing database is 3,000.78. The results are:
3000.78
without the initial comma(,)
.