如何在JavaScript中使用除法 [英] How to use division in JavaScript
本文介绍了如何在JavaScript中使用除法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用JavaScript除以一个数字,它会返回一个十进制值。
I want to divide a number in JavaScript and it would return a decimal value.
例如: 737/1070
-我希望JavaScript返回 0.68
;但是它会一直四舍五入并将其返回为 0
。
For example: 737/1070
- I want JavaScript to return 0.68
; however it keeps rounding it off and return it as 0
.
如何设置它以返回两个小数位数还是完整的结果?
How do I set it to return me either two decimals place or the full results?
推荐答案
使其中一个数字成为浮点数。
Make one of those numbers a float.
737/parseFloat(1070)
或快一点:
737*1.0/1070
转换为小数点后两位
Math.round(737 * 100.0 / 1070) / 100
这篇关于如何在JavaScript中使用除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文