Challenge with Math.floor in Cash Register Project

Hello guys,
I’m using the code below to find out currency needed. This is how much do I need from a particular currency compartment of the cid. For instance, currency unit amount is ‘PENNY’ is $0.01 and current compartment in the cid is [‘PENNY’, 1.01]. It works well using the expression below.

let currencyNeeded = Math.floor(change/currencyUnitAmount);

However, Math.floor(0.039999/0.01) is returning 3 instead 4. So deducting 0.03 from the PENNY compartment instead of 0.04. This my challenge.
I’ve gone to the MDN JavaScript documentation and other document. I haven’t any way around it so far. Please, assist me.

Floating point numbers always have roundoff. I’d use an integer number of cents instead.

1 Like

Thanks. Please, how do you use an integer number of cents?

When I did this challenge, I multiplied everything by 100 and used floor to get integers, then before I returned the result I divided everything by 100 to go back to a decimal number of dollars.

1 Like

Thanks. I’ll try it out

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.