About JavaScript Number type

const calculateWeight = (earthWeight, planet) => {
switch (planet) {
case ‘Mercury’:
return earthWeight * 0.378;
case ‘Venus’:
return earthWeight * 0.907;
case ‘Mars’:
return earthWeight * 0.377;
case ‘Jupiter’:
return earthWeight * 2.36;
case ‘Saturn’:
return earthWeight * 0.916;
default:
return ‘Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.’
}
}
console.log(calculateWeight(100, '‘Saturn’));//91.60000000000001

In this lesson, I found that when I entered the calculation result of’Saturn’, I got 91.60000000000001 instead of 91.6. After reading the number-related information, I still didn’t figure it out. For example,When I counted about ”0.1“ I just need to remove the beginning. Is 0 to avoid calculation errors?

1 Like

This has to do with floating point arithmetic:

https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript