Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Foundations Numbers Creating Numbers: Part 1

Dividing Float point numbers

As Jim shows in this video the multiplication of 0.1 with 0.2 produces a number with a 4 on the far end gives me a spin for my money...

If we take an example: if ((0.1 * 0.2) === 0.02)

This would then evaluate to FALSE, right? I know the 3x = is a strict comparison but the question still remains?

2 Answers

The point that Nejc has tried to get at is that Javascript (for whatever reason) evaluates the expression 0.1 * 0.2 to be 0.020000000000000004. This is because of the way that Javascript maps decimal numbers. There are several ways around this:

  • The easiest method is to truncate at the highest precision you think you'll need. If you're working with money, you only need precision to two decimal points so you could use:
    var a = 0.1;
    var b = 0.2;
    var c = a * b;
    //here is where the number is truncated and converted back into a number type
    if (Number(c.toFixed(2)) === 0.02) {
      //output here
    }

Note: toFixed(#) truncates the variable at the given # but also converts it into a string. In order to do a strict comparison, the two arguments must both be of the same type (in this case, numbers) regardless of the value inside the variable. You could do a == comparision without using the Numbers() conversion and the if-statement would evaluate to true. It's up to you.

  • If you need more precision: You can truncate the expected answer to the amount of expected decimal points i.e. NumberOfDecimalPoints(x) + NumberOfDecimalPoints(y) = ExpectedNumberOfDecimalPoints when using multiplication. So, if we were to multiply 0.1 * 0.2, each has 1 decimal place so the expected number of decimal points in the answer is 2. So, you would use the Number(var.toFixed(#)) and # could be a variable conversion method to compare.

I hope that helps.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Do you mean multiplying floating point numbers in your title?

It looks to me like the expresson evaluates to true anyway and since both values are the same data type strict wouldn't have much effect, :-)

"Do you mean multiplying floating point numbers in your title?"

My bad.

But doesn't the bug prevent the real comparison to complete correctly?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Maybe I'm being thick but I'm not sure where the bug is as the result of the expression is correct and as we say, there's no effect of strict comparisons on the datatypes.

The result of the expression remains a floating point number so ther's no issue there either. There's integer and floating point values.

But as I say, maybe I'm not understading or someone can come in with some points I'm not getting. :-)