Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 13th, 2007, 10:46 AM   #1
peace_of_mind
Professional Programmer
 
peace_of_mind's Avatar
 
Join Date: Sep 2004
Location: Hell if I know most of the time
Posts: 439
Rep Power: 5 peace_of_mind is on a distinguished road
Send a message via MSN to peace_of_mind Send a message via Yahoo to peace_of_mind
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

peace_of_mind is offline   Reply With Quote
Old Aug 13th, 2007, 11:55 AM   #2
peace_of_mind
Professional Programmer
 
peace_of_mind's Avatar
 
Join Date: Sep 2004
Location: Hell if I know most of the time
Posts: 439
Rep Power: 5 peace_of_mind is on a distinguished road
Send a message via MSN to peace_of_mind Send a message via Yahoo to peace_of_mind
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

peace_of_mind is offline   Reply With Quote
Old Aug 13th, 2007, 1:37 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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:
Originally Posted by Output
Owed: 5.07, Paid: 10
Change: 4.93
Dollars: 4
Quarters: 3
Dimes: 1
Nickels: 1
Pennies: 3
__________________
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
DaWei is offline   Reply With Quote
Old Aug 13th, 2007, 2:02 PM   #4
peace_of_mind
Professional Programmer
 
peace_of_mind's Avatar
 
Join Date: Sep 2004
Location: Hell if I know most of the time
Posts: 439
Rep Power: 5 peace_of_mind is on a distinguished road
Send a message via MSN to peace_of_mind Send a message via Yahoo to peace_of_mind
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

peace_of_mind is offline   Reply With Quote
Old Aug 13th, 2007, 5:18 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Aug 13th, 2007, 8:59 PM   #6
peace_of_mind
Professional Programmer
 
peace_of_mind's Avatar
 
Join Date: Sep 2004
Location: Hell if I know most of the time
Posts: 439
Rep Power: 5 peace_of_mind is on a distinguished road
Send a message via MSN to peace_of_mind Send a message via Yahoo to peace_of_mind
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

peace_of_mind is offline   Reply With Quote
Old Aug 14th, 2007, 3:48 PM   #7
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3 Seif is on a distinguished road
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.
Seif is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC