Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Division of float numbers in JavaScript

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?

like image 266
mmrn Avatar asked Jul 27 '13 21:07

mmrn


People also ask

How do you divide float values?

The single forward slash / operator is known as float division, which returns a floating point value. On the other hand, the double forward slash // operator returns a floored value, specifically either a floored integer or floating point value.

How do you divide numbers in JavaScript?

Division (/)The division operator ( / ) produces the quotient of its operands where the left operand is the dividend and the right operand is the divisor.

Can you divide float by INT?

You can divide a floating point number with integer.

What is a floating division?

Float division returns a floating point approximation of the result of a division. For example, . Only a certain number of values after the decimal can be stored, so it is not possible to store an exact binary representation of many floating point numbers.


1 Answers

You can specify the precision of the division:

var width = (100 / 3).toPrecision(3);
like image 97
Derick Leony Avatar answered Sep 25 '22 21:09

Derick Leony