Javascript parse float忽略了逗号后的小数 [英] Javascript parse float is ignoring the decimals after my comma
本文介绍了Javascript parse float忽略了逗号后的小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个简单的场景。我想在我的网站上显示两个值的减法:
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?
推荐答案
这是按设计。 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.
- https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseFloat
要解决此问题,请将逗号转换为小数点。
To fix this convert the commas to decimal points.
var fullcost = parseFloat($("#fullcost").text().replace(',', '.'));
这篇关于Javascript parse float忽略了逗号后的小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文