Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to multiply in Javascript? problems with decimals

i've the following code in Javascript:

var m1 = 2232.00; var percent = (10/100); var total = percent*m1; alert(total); 

The problem is that the variable "total" gives me "223.20000000000002" and it should be "223.2", what should i do to get the correct value?

like image 215
gustavomanolo Avatar asked Jan 23 '13 22:01

gustavomanolo


People also ask

How do I multiply with decimals?

To multiply decimals, first multiply as if there is no decimal. Next, count the number of digits after the decimal in each factor. Finally, put the same number of digits behind the decimal in the product.

How do you multiply in JavaScript?

Multiplication and division operators are also available in JavaScript, and are used to find the product and quotient of numerical values. An asterisk ( * ) is used to represent the multiplication operator.

Can you use decimals in JavaScript?

JavaScript has only one type of number. Numbers can be written with or without decimals.

How to multiply two decimal numbers in JavaScript?

But it can also be tricky if you are trying to multiply 2 decimal numbers that are coming from 2 different input fields. Almost in every programming language, we have a multiplication operator ( *) to multiply one number to another and it is also present in javascript.

Why can’t I use decimal numbers in JavaScript?

But why is that? In JavaScript all numbers are IEEE 754 floating point numbers. Due to the binary nature of their encoding, some decimal numbers cannot be represented with perfect accuracy. This is analagous to how the fraction 1/3 cannot be accurately represented with a decimal number with a finite number of digits.

Does JavaScript solve all problems with numbers?

Of course, this doesn't solve all problems. Division of integers and multiplication by decimals may still result in inexact values, but basic integer addition, subtraction, and multiplication will be accurate. (At least until you hit JavaScript's upper limit for numbers.)

Why are my numbers not divisible by 2 in JavaScript?

This happens because Javascript treats all of its numbers as floating point numbers, and their binary math cannot exactly represent numbers which has denominator not divisible by 2. See this excellent StackOverFlow post for an explanation. And, see this by the author of the selected answer. Binary floating point math is like this.


2 Answers

.toFixed() is best solution.It will keep only two digits after dot.

Exp 1:

var value = 3.666; value.toFixed(2); //Output : 3.67 

Exp 2:

var value = 3.0000; value.toFixed(2); //Output : 3.00 
like image 103
Malik Khalil Avatar answered Sep 18 '22 13:09

Malik Khalil


You can't get the exact value. This is the fundamental problem with floating-point numbers.

You can force a fixed number of decimal numbers with toFixed:

alert(total.toFixed(2)); 

However, keep in mind that this will leave trailing zeroes, which you might not want. You can remove them with .replace(/0+$/,'');

like image 30
Niet the Dark Absol Avatar answered Sep 21 '22 13:09

Niet the Dark Absol