❔ Coin sorting issue
Hey guys, I'm building a little test app that gives you the smallest amount of coins that you can use to make a specific value. Here is the program running, and here is my code
I'm having an issue, where when it gets to a certain number, it's breaking. Do I need to use Ceiling or Floor in the code? I just find it weird as I swear I built this exact same program with roughly the same code in Uni and it worked?
Every if/else if statement is the same as these, and then it prints out the coins, but due to the weird issue, it never gets to 0. I guess I could add an if statement to check if it is less than 0.01
Thank you
10 Replies
I suggest looking into the modulus operator.
And you are not showing all the code so I can't tell why your current implementation is failing.
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/BlazeBin - elwkqappayom
A tool for sharing your source code with the world!
You're probably just running into floating point precision errors.
ahh, okay, so I should use Floor, or Ceil?
It happened when I converted it all to doubles too
You should use the modulus operator.
This is a standard problem in Computer Science: https://www.geeksforgeeks.org/find-minimum-number-of-coins-that-make-a-change/
GeeksforGeeks
Find minimum number of coins to make a given value (Coin Change) - ...
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
i would advise against using float and instead using decimal type
for money
it's the type made specifically for this case
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.