在JavaScript中乘法int和float(double) [英] Multiplication int and float(double) in JavaScript

查看:85
本文介绍了在JavaScript中乘法int和float(double)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 JavaScript 进行乘法运算。

I want to make multiplication using JavaScript.

2和0.15的乘法是0.3但是3和0.15是0.44999999999999996。我想得到0.45如结果。如何使用 JavaScript?

Multiplication of 2 and 0.15 is 0.3 but 3 and 0.15 is 0.44999999999999996. I want to get 0.45 such as result. How can I do it with JavaScript?

推荐答案

这是一个舍入误差。您可能希望将数字四舍五入为固定的数字位数,如下所示:

It's a rounding error. You may want to round your number to a fixed amount of digits, like this:

parseFloat((your_number).toFixed(2));

这篇关于在JavaScript中乘法int和float(double)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆