Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is the content of the res variable in the following statement : var res = 0.1 * 3;?

Why we don't have the result expected and how to gracely fix it ?

user-image
Question added by Jonathan de Flaugergues , software engineer , Abbeal
Date Posted: 2017/01/17
Ahmad Al-Mutawa
by Ahmad Al-Mutawa , Lead Engineer, System Application , Saudi Iron & Steel Company (HADEED)

The logical value should be 0.3

 

but once the code is executed, we can see the value 0.30000000000000004

 

The source of this issue is due to the value (0.1) being an irrational number.

 

To fix this and get the expected value, one way is to do this:

 

var res=(0.1*3).toFixed(1);

 

Output:  "0.3"

 

 

More Questions Like This