ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

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
تم إضافة السؤال من قبل Jonathan de Flaugergues , software engineer , Abbeal
تاريخ النشر: 2017/01/17
Ahmad Al-Mutawa
من قبل 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"

 

 

المزيد من الأسئلة المماثلة