JavaScript中浮点数的划分 [英] Division of float numbers in JavaScript
本文介绍了JavaScript中浮点数的划分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有什么好的解决方法,这个问题?
解决方案
var width =(100/3).toPrecision(3) ;
I'm trying to divide screenwidth by a variable in order to draw equally spaced UI parts in webview.
However, when the variable is 3, 100 / 3 results 33.3333333333333336 in JavaScript, and the third part cannot be drawn as intended because the sum of the quotients is bigger than 100%, I gusss.
Is there any nice workaround for this problem?
解决方案
You can specify the precision of the division:
var width = (100 / 3).toPrecision(3);
这篇关于JavaScript中浮点数的划分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文