javascript 在 jQuery 中添加浮点值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19287654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:00:00  来源:igfitidea点击:

Adding Float value in jQuery

javascript

提问by Dhamu

Adding float values using jQuery

使用 jQuery 添加浮点值

used code:

使用的代码:

result variables:

结果变量:

subtotal = 4.6;
tax = 2.3;

Total = parseFloat(subtotal)+parseFloat(tax);

i got result 6.8999999999999995... why it not returning the correct value 6.9

我得到了结果 6.8999999999999995...为什么它没有返回正确的值 6.9

回答by Rohan Kumar

Use toFixed()like

使用toFixed() 之类的

Total =Total.toFixed(1);

Or Simply,

或者Simply

Total = (parseFloat(subtotal)+parseFloat(tax)).toFixed(1);