0

I can't seem to find a way to return a number as 0.50 not 0.5

I found various solutions to display always 2 digits but all of them work only if the number ends in something other than 0 e.g. 0.05 works fine, but 0.20 will always be returned as 0.2

Again, I need typeof(number) to be number NOT string.

I am trying to solve this: freecodecamp.org/challenges/exact-change and I need to return [["TWENTY", 60.00], ["TEN", 20.00], ["FIVE", 15.00], ["ONE", 1.00], ["QUARTER", 0.50], ["DIME", 0.20], ["PENNY", 0.04]] -- if I return for example ["QUARTER", "0.50"] it will not pass

Paul
  • 139,544
  • 27
  • 275
  • 264
Sasha
  • 65
  • 9
  • It's impossible without converting to a string before displaying/outputting it. A number isn't stored internally as "0.5" or "0.50", and there is no reason to worry about trailing 0's except when you're outputting it as a string anyway. – Paul Oct 28 '17 at 15:42
  • Show your code. – skyline3000 Oct 28 '17 at 15:43
  • @Sasha number types don't store anything about how a number should be displayed, they only store information about the numeric value, and since 0.5 and 0.50 are the same numeric value, there is only one representative number. You can't control how it's displayed until you convert it to a string. – Paul Oct 28 '17 at 15:52
  • I edited my original question with more clarification about what I'm trying to achieve. – Sasha Oct 28 '17 at 15:54
  • Thanks for sharing the context, I can see that they made it a bit confusing. I think you should not worry about the trailing `0`. I believe the test will pass if you return the correct output even though your output will have numbers like `0.5`. That should be fine. – Paul Oct 28 '17 at 15:58
  • I will try my hand at the challenge to make sure that it accepts that output. – Paul Oct 28 '17 at 15:59
  • @Paulpro you can paste this code: https://pastebin.com/9aDZCWuP in that validator, you'll see what you mean. (i know the code is not the most efficient one ever, i'm still learning) – Sasha Oct 28 '17 at 16:06
  • @Sasha I solved it with different code. You can check it if you want: https://pastebin.com/Ud6dfkUy . The main thing to notice is just that the tester doesn't care that the output has numbers like `0.4` in it. The tests pass anyway. – Paul Oct 28 '17 at 16:45
  • @Sasha If I run your code I find that it fails for a separate reason. You are returning a 1 dimensional array, but the tester expects a 2D array as output (same format as the `cid` input array)`. – Paul Oct 28 '17 at 16:48
  • @Sasha As an example, when you return change, you return something like: – Paul Oct 28 '17 at 16:49
  • `["TWENTY",60,"TEN",20,"FIVE",15,"ONE",1,"QUARTER","0.50","DIME",0.2,"PENNY",0.04]` – Paul Oct 28 '17 at 16:49
  • @Sahsa But the test expects: `[["TWENTY",60],["TEN",20],["FIVE",15],["ONE",1],["QUARTER","0.50"],["DIME",0.2],["PENNY",0.04]]` – Paul Oct 28 '17 at 16:49
  • The `0.2` for the amount of DIME's is correct, but it needs to be a 2D array – Paul Oct 28 '17 at 16:50

0 Answers0