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?
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.
Division (/)The division operator ( / ) produces the quotient of its operands where the left operand is the dividend and the right operand is the divisor.
You can divide a floating point number with integer.
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.
You can specify the precision of the division:
var width = (100 / 3).toPrecision(3);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With