Displaying the wrong results
Hi
I'm attempting the following code to calculate how many quarters, dimes, nickels and pennies a user should get as change, and display them in four labels, when buying an item from a machine. The user inserts the amount paid and the cost of the item in two textboxes.
My code is as follows:
Code:
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
' Variables for fields
Dim lblCost As Double = CDbl(txtCostItm.Text)
Dim lblAmnt As Double = CDbl(txtAmntGvn.Text)
' Find Difference between Cost of Item and Price Received
Dim temp As Double = txtAmntGvn.Text - txtCostItm.Text
'Declare Integers
Dim lblQuar As Integer
Dim lblDim As Integer
Dim lblNick As Integer
Dim lblPenn As Integer
'How many quarters will be handed
lblQuar = CInt(temp / 25)
temp = temp - CDbl(CDbl(lblQuar) * 25)
lblQrts.Text = lblQuar.ToString
'Dimes
lblDim = CInt(temp / 10)
temp = temp - CDbl(CDbl(lblDim) * 10)
lblDimes.Text = lblDim.ToString
'Nickels
lblNick = CInt(temp / 5)
temp = temp - CDbl(CDbl(lblNick) * 5)
lblNckls.Text = lblNick.ToString
'Pennies
lblPenn = CInt(temp / 1)
temp = temp - CDbl(CDbl(lblPenn) * 1)
lblPennies.Text = lblPenn.ToString
I cant understand where I'm going wrong... anyone?!
Thanks a lot, in advance.
Re: Displaying the wrong results
Try using mod & truncate division. Also the code will only work if the difference amount is specified in pennies
Code:
Dim lblCost As Double = CDbl(Txtcostitm.Text)
Dim lblAmnt As Double = CDbl(txtAmntGvn.Text)
' Find Difference between Cost of Item and Price Received
Dim temp As Double = txtAmntGvn.Text - Txtcostitm.Text
'How many quarters will be handed
lblQrts.Text = Math.Truncate(temp / 25)
temp = temp Mod 25
'Dimes
lblDimes.Text = Math.Truncate(temp / 10)
temp = temp Mod 10
'Nickels
lblNckls.Text = Math.Truncate(temp / 5)
temp = temp Mod 5
'Pennies
lblPennies.Text = temp
Maybe you've to change something, buy hope it helps as a base.
Re: Displaying the wrong results
If there is a decimal expected in the calcuation result, then the integer variable will ignore it. Put break point and step through the program to see where you are going wrong?
Re: Displaying the wrong results
Code:
Debug.WriteLine("Calculate Change")
Dim decAMT As Decimal = 0.73D 'change
Dim dec25, dec10, dec5, dec1 As Decimal 'quarters, dimes,nickles, pennies
'calculate change for amount using Decimal
dec25 = Math.Floor(decAMT / 0.25D)
decAMT -= dec25 * 0.25D
dec10 = Math.Floor(decAMT / 0.1D)
decAMT -= dec10 * 0.1D
dec5 = Math.Floor(decAMT / 0.05D)
decAMT -= dec5 * 0.05D
dec1 = Math.Floor(decAMT / 0.01D)
decAMT -= dec1 * 0.01D
Debug.WriteLine("Pennies " & dec1) 'should be 3
Debug.WriteLine("Nickles " & dec5)
Debug.WriteLine("Dimes " & dec10)
Debug.WriteLine("Quarters " & dec25)
Debug.WriteLine(decAMT) 'should be 0
Re: Displaying the wrong results
Quote:
Originally Posted by
Oblio
Code:
Debug.WriteLine("Calculate Change")
Dim decAMT As Decimal = 0.73D 'change
Dim dec25, dec10, dec5, dec1 As Decimal 'quarters, dimes,nickles, pennies
'calculate change for amount using Decimal
dec25 = Math.Floor(decAMT / 0.25D)
decAMT -= dec25 * 0.25D
dec10 = Math.Floor(decAMT / 0.1D)
decAMT -= dec10 * 0.1D
dec5 = Math.Floor(decAMT / 0.05D)
decAMT -= dec5 * 0.05D
dec1 = Math.Floor(decAMT / 0.01D)
decAMT -= dec1 * 0.01D
Debug.WriteLine("Pennies " & dec1) 'should be 3
Debug.WriteLine("Nickles " & dec5)
Debug.WriteLine("Dimes " & dec10)
Debug.WriteLine("Quarters " & dec25)
Debug.WriteLine(decAMT) 'should be 0
I tried to replace Math.Floor with \ division, but it throws a division by zero, even when the divisor is not zero (?).
It was looking simpler to do a class with it, but in the end, it turned more complex:
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyCoinChange As New CoinChange
For Each CoinName As String In MyCoinChange.GetChange(15.16).Keys
Debug.WriteLine(CoinName.PadRight(9) & ":" & MyCoinChange.GetChange()(CoinName))
Next CoinName
End Sub
Private Class CoinChange
Friend CoinsValue As New Dictionary(Of String, Double)
Friend Sub New()
With CoinsValue
.Add("Quarters", 25 / 100)
.Add("Dimes", 10 / 100)
.Add("Nickles", 5 / 100)
.Add("Pennies", 1 / 100)
End With
End Sub
Friend ReadOnly Property GetChange(Optional ByVal Mount As Double = Double.NaN) As Dictionary(Of String, Double)
Get
Static Answer As Dictionary(Of String, Double)
If Double.IsNaN(Mount) Then
Return Answer
Else
Answer = New Dictionary(Of String, Double)
End If
For Each CoinName As String In CoinsValue.Keys
Dim CoinCount As Integer
CoinCount = CInt(Math.Floor(Mount / CoinsValue(CoinName)))
Answer.Add(CoinName, CoinCount)
Mount -= CoinCount * CoinsValue(CoinName)
Next CoinName
Return Answer
End Get
End Property 'GetChange
End Class 'CoinChange
End Class
The class can also count the remaining coins in the store.
Re: Displaying the wrong results
"I tried to replace Math.Floor with \ division, but it throws a division by zero, even when the divisor is not zero (?)."
That is Integer division so things like 0.05D are getting rounded to ... 0
Re: Displaying the wrong results
Quote:
Originally Posted by
Oblio
"I tried to replace Math.Floor with \ division, but it throws a division by zero, even when the divisor is not zero (?)."
That is Integer division so things like 0.05D are getting rounded to ... 0
Oh. I see.
I were thinking that on this age, any x86 processor contains the "\" operator supported in hardware for any floating point format...
There are so many useless instructions...