Couple of issues to address: 1. Use Decimal instead of Double or Single because of the inherent inaccuracy of floating point types.
2. Avoid using functions that are there only for backward compatibilty sake (such as "isNumeric", "FormatCurrency", etc.). Use .NET framework functions instead. 3. Avoid the use of VB's type conversion functions for parsing. (Note that I strongly recommend their use when converting datatypes, just not during string parsing). 4. Your logic isn't all that flawed except for the use of Double datatypes and the fact that integral conversion functions such as CInt () don't just remove the fractional part but also perform a rounding to the nearest integer. This causes values such as 0.65 to be rounded to 1. The better option is to use Truncate() - because it rounds downward tending towards zero. See the following sample : --- intDollarsBack = CInt(Decimal.Truncate(change / DOLLAR)) change -= (intDollarsBack * DOLLAR) intQuartersBack = CInt(Decimal.Truncate(change / QUARTER)) change -= (intQuartersBack * QUARTER) intDimesBack = CInt(Decimal.Truncate(change / DIME)) change -= (intDimesBack * DIME) intNickelsBack = CInt(Decimal.Truncate(change / NICKEL)) change -= (intNickelsBack * NICKEL) intPenniesBack = CInt(Decimal.Truncate(change / PENNY)) change -= (intPenniesBack * PENNY) --- 5. You can also use a Dictionary type collection and loop through that instead of having to write the repetitive code above. But that's another topic altogether. On Sep 17, 7:37 pm, Nathan <[email protected]> wrote: > I have most of my program working, I just am having trouble tring to > calculate the change due back to customer. I need the exact change > like 2 dollars 3quarters..ect I think my logic is way off... > > here is my code..... > > Private Sub btnCalOrder_Click(ByVal sender As System.Object, ByVal e > As System.EventArgs) Handles btnCalOrder.Click > Dim customers_wallet As Double > Dim customer_total As Double > > Dim dblChangeAmount As Double > Dim intDollarsBack As Integer > Dim intQuartersBack As Integer > Dim intDimesBack As Integer > Dim intNickelsBack As Integer > Dim intPenniesBack As Integer > Const dblDollarValue As Double = 1.0 > Const dblQuarterValue As Double = 0.25 > Const dblDimeValue As Double = 0.1 > Const dblNickelValue As Double = 0.05 > Const dblPennyValue As Double = 0.01 > Dim change_back As Double > > If IsNumeric(txtAmtTen.Text) Then > > customers_wallet = CDbl(txtAmtTen.Text) 'set Amount > Tendered to that var > customer_total = CDbl(txtTotal.Text) - customers_wallet ' > total - what cus owes > txtChange.Text = FormatCurrency(customer_total) 'format > currency and place in change textbox > > change_back = CDbl(txtChange.Text) > dblChangeAmount = change_back 'extact change due to > customer > > intDollarsBack = CInt(dblChangeAmount / dblDollarValue) > dblChangeAmount = dblChangeAmount - (CDbl(intDollarsBack) > * dblDollarValue) > > intQuartersBack = CInt(dblChangeAmount / dblQuarterValue) > dblChangeAmount = dblChangeAmount - (CDbl(intQuartersBack) > * dblQuarterValue) > > intDimesBack = CInt(dblChangeAmount / dblDimeValue) > dblChangeAmount = dblChangeAmount - (CDbl(intDimesBack) * > dblDimeValue) > > intNickelsBack = CInt(dblChangeAmount / dblNickelValue) > dblChangeAmount = dblChangeAmount - (CDbl(intNickelsBack) > * dblNickelValue) > > intPenniesBack = CInt(dblChangeAmount / dblPennyValue) > dblChangeAmount = dblChangeAmount - (CDbl(intPenniesBack) > * dblPennyValue) > > 'change_cents = customer_total / 100 > 'change_num_coins = CLng(change_cents) \ 3 > 'change_remainder = CLng(change_num_coins) Mod 3 > 'lblfinal.Text = intDollarsBack & " Dollars" & > intQuartersBack & " Quarters" & intDimesBack & " Dimes" & > intNickelsBack & " Nickels" & intPenniesBack & " Pennies" > lbltest.Text = CStr(num_pennies) > Else > MsgBox("Invaild Character") > End If > > End Sub
