![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
|
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.
__________________
Amateurs built the ark Professionals built the Titanic |
|
|
|
|
|
#2 |
|
Professional Programmer
|
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 Dim dollars, quarters, dimes, nickels, pennies as integer owedAmount = Val(Me.uiOwedAmountTextBox.Text) paidAmount = Val(Me.uiPaidAmountTextBox.Text) change = paidAmount - owedAmount temp = change dollars = (temp * 100) \ 100 temp = temp - dollars quarters = temp * 100 \ 25 temp = temp - quarters * .25 etc.
__________________
Amateurs built the ark Professionals built the Titanic |
|
|
|
|
|
#3 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
This is VB, POM, but you can see the flow:
#include <iostream>
using std::cout;
using std::endl;
int main ()
{
double owedAmount = 5.07;
double paidAmount = 10.00;
double change;
int dollars, quarters, dimes, nickels;
change = paidAmount - owedAmount;
cout << "Owed: " << owedAmount << ", Paid: " << paidAmount << endl;
cout << "Change: " << change << endl;
dollars = change / 1;
change = change - dollars;
quarters = change / .25;
change = change - quarters * .25;
dimes = change / .10;
change = change - dimes * .10;
nickels = change / .05;
change = change - nickels * .05;
change = change / .01;
cout << "Dollars: " << dollars << endl;
cout << "Quarters: " << quarters << endl;
cout << "Dimes: " << dimes << endl;
cout << "Nickels: " << nickels << endl;
cout << "Pennies: " << change << endl;
return 0;
}Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#4 |
|
Professional Programmer
|
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...
__________________
Amateurs built the ark Professionals built the Titanic |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Professional Programmer
|
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
Dim dollars, quarters, dimes, nickels, pennies As Integer
owedAmount = Me.uiOwedAmountTextBox.Text
paidAmount = Me.uiPaidAmountTextBox.Text
change = paidAmount - owedAmount
temp = change
dollars = (temp * 100) \ 100
temp = temp - dollars
quarters = (temp * 100) \ 25
temp = temp - quarters * 0.25
dimes = (temp * 100) \ 10
temp = temp - dimes * 0.1
nickels = (temp * 100) \ 5
temp = temp - nickels * 0.05
pennies = (temp * 100) \ 1
temp = temp - pennies * 0.01
__________________
Amateurs built the ark Professionals built the Titanic |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| vb.net to VBScript | randum77 | JavaScript and Client-Side Browser Scripting | 3 | May 1st, 2007 3:39 PM |
| Help with Loop and Calculation | Barb | C | 14 | Apr 10th, 2006 7:15 PM |
| difference VB and VB.net | gpreetsingh | Visual Basic .NET | 10 | Feb 18th, 2006 6:18 AM |
| SQL 'LIKE' problems in VB.NET with OleDb | lectricpharaoh | Visual Basic .NET | 0 | Dec 5th, 2005 1:15 AM |
| Making a database application in VB.NET | emdiesse | Visual Basic .NET | 3 | Sep 18th, 2005 5:58 AM |