How can I convert an integer to two decimal place in Javascript?

Hi all,

Can someone please explain to me how one is supposed to convert an integer, say 5 to 5.00 in javascript? I need the converted value to be of type Number, not a string (toFixed wont work for this).

I have my final advanced algorithm challenge (exact change) done but I can’t get the formatting of the numbers correct (they are currently strings instead of type Number).

I have tried nearly everything I’ve read on stacked overflow but it either A) doesn’t work or B) will return the correct value as a string instead of a Number.

Is there seriously no way to go 3 -> 3.00 instead of 3 -> “3.00” in javascript?

1 Like

Are you failing one of the tests because of this inability to state 5.00 instead of 5? I ask because my answer for the example test was [ [ ‘QUARTER’, 0.5 ] ]. Even though is does not show [ [ ‘QUARTER’, 0.50 ] ], it will still pass the test, because 0.5 is the same as 0.50.

1 Like

Hi @rmdawson71

I did assume I was failing because of the type check. For instance

checkCashRegister(3.26, 100.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]])

should return
[["TWENTY", 60.00], ["TEN", 20.00], ["FIVE", 15.00], ["ONE", 1.00], ["QUARTER", 0.50], ["DIME", 0.20], ["PENNY", 0.04]]

my (failing) solution returns:
[ [ ‘TWENTY’, ‘60.00’ ],
[ ‘TEN’, ‘20.00’ ],
[ ‘FIVE’, ‘15.00’ ],
[ ‘ONE’, ‘1.00’ ],
[ ‘QUARTER’, ‘0.50’ ],
[ ‘DIME’, ‘0.20’ ],
[ ‘PENNY’, ‘0.04’ ] ]

You just need to not convert the dollars & cents part to text and you should be fine. It is expecting a number and not a string for the second part of each sub array.

1 Like

Thanks very much, that worked :slight_smile: