很抱歉,但我需要帮助
我已经尝试将字符串转换为十进制,它可以工作,但我有一些问题:
number = document.getElementById("totalcost").innerHTML; //It is a string, but I am sure that it is a decimal
number2 = prodCost; //it is a string but in fact it is a decimal too
alert(parseFloat(number)); // prints good (if number is 88,9 it will print 88,9)
alert(parseFloat(number2)); // it's ok too
alert(parseFloat(number) - parseFloat(number2)); // this is not ok :(
//if number=88,9 and number2=17,77 I get 71 but i need 71,13哦,伙计们,我很抱歉,我傻了。非常感谢!我已经连续工作了9小时。我很抱歉,谢谢大家!
发布于 2014-03-11 13:37:07
这看起来像是一个区域设置问题:parseFloat只将句点识别为小数点;它在到达逗号时停止解析,只给出整数值。不幸的是,没有办法改变这种行为。为了得到十进制数字,您需要在数字字符串中用句点替换逗号。
发布于 2014-03-11 13:36:42
如果你要用点代替逗号(例如: 71.13而不是71,13),那么一切都会像预期的那样工作。
发布于 2014-03-11 13:37:22
parseInt和parseFloat在提供的字符串中获得第一个有效数字。
,无效
parseFloat("17,77".replace(",","")); //1777如果用逗号是分隔符的话。
或者如果用逗号作为小数点
parseFloat("17,77".replace(",",".")); //17.77在MDN 这里中解释
如果parseInt遇到的字符不是指定基数中的数字,它将忽略该字符和所有后续字符,并返回解析到此点的整数值。parseInt将数字截断为整数值。允许前导和尾随空间。
https://stackoverflow.com/questions/22327225
复制相似问题