JavaScript中浮点数的划分 [英] Division of float numbers in JavaScript

查看:152
本文介绍了JavaScript中浮点数的划分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过变量来划分屏幕宽度,以便在webview中绘制等距的UI部分。然而,当变量为3时,100/3的结果为33.3333333333333336在JavaScript中,第三部分不能按预期绘制,因为商的总和大于100 %,我gusss。



有没有什么好的解决方法,这个问题?

解决方案

  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屋!

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