Javascript浮点数乘以100仍然有错误

编程入门 行业动态 更新时间:2024-10-28 16:22:36
本文介绍了Javascript浮点数乘以100仍然有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在钱栏上输入了文字.

I have a text input for a money field.

我在字段中输入33.91,并在尝试使用乘以100"技术时得到以下结果.

I enter 33.91 into the field, and get the following results while trying to use the 'multiply by 100' technique.

var curWth = parseInt($('#trans_withdraw'+index).val()*100); // 3390 var curWth = parseInt($('#trans_withdraw'+index).val())*100; // 3300 var curWth = $('#trans_withdraw'+index).val()*100; // 3390.9999999...5 var curWth = parseFloat($('#trans_withdraw'+index).val())*100; // 3390.9999999...5 var curWth = parseFloat($('#trans_withdraw'+index).val()*100); // 3390.9999999...5

这些都不给我3391.我认为乘以100的整个想法是要摆脱浮点问题.但是,如第3行所示,当我立即将值乘以100时,仍然会出现浮点错误.

None of these give me 3391. I thought the whole idea of multiplying by 100 was to get away from the floating point problem. But as line 3 shows, when I multiply the value by 100 immediately, I still get floating point errors.

在使用货币时,我仍然不了解什么?

What am I still not understanding about working with currency?

我知道0.1不能正确存储为浮点数.但是在这种情况下,乘以100也不适合我.

I know 0.1 can't be stored correctly as a float. But multiplying by 100 is also not working for me in this case.

推荐答案

praseFloat to get your decimal value then multiple by 100 to get 3390.9999 then Math.round() to get the value.

var number = parseFloat($('#trans_withdraw').val()).toFixed(2); var value = number * 100; console.log(Math.round(value));

<script src="ajax.googleapis/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="trans_withdraw" value="33.91" />

更多推荐

Javascript浮点数乘以100仍然有错误

本文发布于:2023-11-10 06:53:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574657.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有错误   浮点数   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!