![]() |
Calculation problems with my VB.NET solution
An assignment I'm working on has presented the following problem for me:
I have to calculate the change owed to a customer. No problem: change = amountPaid - amountOwed Here's where I'm having trouble. Not only do I have to display a message stating the change owed, but I have to display how many dollars, quarters, dimes, nickels, and pennies are owed. The logic for these calculations is where I'm getting bogged down. The way I initially approached it was to try: dollars = change / 1 quarters = (change - dollars) / 0.25 dimes = (change - dollars - quarters) / 0.10 etc. The problem with this is, obviously, the decimal remainder when the division occurs. I need a way to divide by each value, and then truncate the decimal remainder, leaving just the whole number for each calculation. This project is in VB.NET. Any thoughts/suggestions would be appreciated. |
Okay I think I have figured it out. If I take the dollar amount and multiply it by 100 I get rid of the decimal. And as long as I multiply the other side of the equation by 100, I can use integer division to solve the problem. I think. When I get home to my source code I'll give this a try. In the meantime if anyone has any other approaches I'd love to hear them.
So the new code would look like: :
Dim owedAmount, paidAmount, change, temp As Single |
This is VB, POM, but you can see the flow:
:
#include <iostream>Quote:
|
Thanks, DaWei. That's interesting. I'll have to go back and look over my original code before I try my idea from post #2. I used the same equations that you did, but I believe my variables were declared differently. Guess I gots some tinkering to do...
|
I would have mentioned fmod, but I don't know if VB has a similar function. The germane thing is that a float, when stuffed into an int, will lose its decimal places. You need to be aware that a floating point value cannot possibly represent all the values that lie in its range. That's because a value comprising a given number of bits can only represent 2^nbits distinct things. When only dealing with two decimal places, though, you should be reasonably safe.
|
Thanks again. Once again I've learned something today.
For anyone interested here's how my code ended up. I'm rather pleased with it and I believe it's the approach the instructor was looking for. :
Dim owedAmount, paidAmount, change, temp As Decimal |
glad you are pleased, must of taken all day to take out them there semi colons ;)
In VB.Net you can set the precision of the Decimal data type. Also if you just wanted to display the number you can do FormatNumber() function. There is also ParseNumber() if you require the number from the previous string at any point. Not as efficient as doing it Daweis way by any means but just an alternative for your consideration. |
| All times are GMT -5. The time now is 2:56 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC