Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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 ajoutée par Jonathan de Flaugergues , software engineer , Abbeal
Date de publication: 2017/01/17
Ahmad Al-Mutawa
par 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