In your example of $2.99, look at where you calculate inDollarsBack
. You are obviously getting a value of 3, when you need 2. Without giving it away, think of possible reasons why dividing 2.99 by 1 would produce a value of 3.
+1
A:
Larsenal
2010-09-24 23:21:34
Thank you, That makes perfect sense.....it's rounding. I need to drop the decimal place off......but how do I do that?
BlueBeast
2010-09-24 23:40:15
Edit: check out this list of Math functions: http://msdn.microsoft.com/en-us/library/system.math_members%28v=VS.71%29.aspx
Larsenal
2010-09-25 00:05:50
Thanks for pointing me in the right direction. I figured a way out.....but there must be an easier way. Posted it at bottom of OP.
BlueBeast
2010-09-25 00:12:24
Well, my method worked perfectly when there are coins involved......but now even numbers show -1 quarter and 2 dimes and 5 pennies.....which makes sense why it would because of the way that I wrote it. VB is very new to me. This is my first assignment and my professor is HORRIBLE(he just plays Freecell while we read the book), so I'm really lost.
BlueBeast
2010-09-25 00:56:58
Try this function out: http://msdn.microsoft.com/en-us/library/system.math.floor(v=VS.71).aspxAlso, when dealing with money it is sometimes useful to multiply by 100 to get the entire amount in pennies. Then you don't have to deal with decimals during the math.
Ron Savage
2010-09-25 01:09:59
You are referring to the floor/ceiling right? The problem is that we haven't learned about it in class yet and he will say that it is not the correct way to write it. If I multiply by 100, wouldn't it still round though?
BlueBeast
2010-09-25 16:09:15
I appreciate the responses so far, and I'm sure your solutions would work great. However, I'm a complete noob to VB and there has to be a simple, easy way to do this. I am already doing things that we haven't learned in class yet. This is supposed to be an easy assignment and I think I'm making it too complicated........anyone have a VERY simple solution.
BlueBeast
2010-09-25 16:14:39
+1
A:
You can use the following change calculator and then update the UI.
Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.VisualBasic
Public Class ChangeCalculator
Private denominations As List(Of Denomination)
Public Sub New()
Me.denominations = New List(Of Denomination)
With Me.denominations
.Add(New Denomination With {.Value = 1D})
.Add(New Denomination With {.Value = 0.25D})
.Add(New Denomination With {.Value = 0.1D})
.Add(New Denomination With {.Value = 0.05D})
.Add(New Denomination With {.Value = 0.01D})
End With
End Sub
Public Sub Calculate(ByVal change As Decimal)
Me.ResetDenominationUnits()
Dim remainingChange = change
For Each denomination In (From d In Me.denominations
Order By d.Value Descending
)
If remainingChange > denomination.Value Then
denomination.Units = CInt(
Conversion.Int(remainingChange / denomination.Value)
)
remainingChange -= denomination.Value * denomination.Units
End If
Next
End Sub
Public ReadOnly Property Dollars As Integer
Get
Return Me.GetUnits(1D)
End Get
End Property
Public ReadOnly Property Quarters As Integer
Get
Return Me.GetUnits(0.25D)
End Get
End Property
Public ReadOnly Property Dimes As Integer
Get
Return Me.GetUnits(0.1D)
End Get
End Property
Public ReadOnly Property Nickels As Integer
Get
Return Me.GetUnits(0.05D)
End Get
End Property
Public ReadOnly Property Pennies As Integer
Get
Return Me.GetUnits(0.01D)
End Get
End Property
Private Function GetUnits(ByVal denomination As Decimal) As Integer
Return (From d In Me.denominations
Where d.Value = denomination
).Single().Units
End Function
Private Sub ResetDenominationUnits()
For Each denomination In Me.denominations
denomination.Units = 0
Next
End Sub
End Class
Public Class Denomination
Public Property Value As Decimal
Public Property Units As Integer
End Class
Tim Murphy
2010-09-26 14:10:21
A:
Inasmuch as my answer's similar to Tim Murphy's, I think this one's more straight forward and you don't need to update the UI
.
Module Module1
Sub Main()
Console.WriteLine("Enter the amount of change or type EXIT to close.")
Dim input = Console.ReadLine
Do While input.ToUpper.Trim <> "EXIT"
Dim c = GetChange(CDec(Val(input)))
Console.WriteLine("{0} dollars, {1} quarters, {2} dimes, {3} nickels and {4} pennies", _
c.Dollars, c.Quarters, c.Dimes, c.Nickels, c.Pennies)
Console.WriteLine(vbCrLf & vbCrLf & "Enter the amount of change or type EXIT to quit.")
input = Console.ReadLine
Loop
End Sub
Public Function GetChange(ByVal change As Decimal) As Change
Dim denominations = New Decimal() {1D, 0.25D, 0.1D, 0.05D, 0.01D}
Dim c(4) As Integer
For i = 0 To denominations.Length - 1
If change >= denominations(i) Then
c(i) = CInt(Conversion.Int(change / denominations(i)))
change -= (c(i) * denominations(i))
End If
Next
Dim r As New Change
With r
.Dollars = c(0)
.Quarters = c(1)
.Dimes = c(2)
.Nickels = c(3)
.Pennies = c(4)
End With
Return r
End Function
Public Structure Change
Dim _dollars As Integer
Dim _quarters As Integer
Dim _nickels As Integer
Dim _dimes As Integer
Dim _pennies As Integer
Public Property Dollars() As Integer
Get
Return _dollars
End Get
Set(ByVal value As Integer)
_dollars = value
End Set
End Property
Public Property Quarters() As Integer
Get
Return _quarters
End Get
Set(ByVal value As Integer)
_quarters = value
End Set
End Property
Public Property Dimes() As Integer
Get
Return _dimes
End Get
Set(ByVal value As Integer)
_dimes = value
End Set
End Property
Public Property Nickels() As Integer
Get
Return _nickels
End Get
Set(ByVal value As Integer)
_nickels = value
End Set
End Property
Public Property Pennies() As Integer
Get
Return _pennies
End Get
Set(ByVal value As Integer)
_pennies = value
End Set
End Property
End Structure
End Module
Alex Essilfie
2010-09-26 20:30:45
Nice solution. However, I don't understand your point of not needing to update the UI. You are updating the UI with calls to Console.WriteLine().
Tim Murphy
2010-09-27 08:24:22
A:
Public Shared Function CalculateChange(ByVal dblDollarsPaid As Double, ByVal dblDollarsOwed As Double)
Dim intChangeCents As Integer
Dim change As New Change()
intChangeCents = CInt((dblDollarsPaid - dblDollarsOwed) * 100)
change.Dollars = intChangeCents \ 100
intChangeCents = intChangeCents Mod 100
change.Quarters = intChangeCents \ 25
intChangeCents = intChangeCents Mod 25
change.Dimes = intChangeCents \ 10
intChangeCents = intChangeCents Mod 10
change.Nickels = intChangeCents \ 5
intChangeCents = intChangeCents Mod 5
change.Pennies = intChangeCents
Return change
End Function
Usage:
Dim c As Change
c = CalculateChange(13.26, 2.99)
Console.WriteLine("{0} dollars, {1} quarters, {2} dimes, {3} nickels and {4} pennies", _
c.Dollars, c.Quarters, c.Dimes, c.Nickels, c.Pennies)
\
is the operator for integer division, e.g. 5 \ 2 is 2, not 2.5.
The Change
structure is from Alex Essilfie's answer.
Cristian Ciupitu
2010-09-27 00:46:34
By the way, the first result of a Google search for *visual basic .net integer division* is the [answer](http://discuss.techinterview.org/default.asp?dotnet.12.155890.2) to the question. That's how I knew how to do it.
Cristian Ciupitu
2010-09-27 10:31:45
A:
SOLVED- solution posted in OP.
Thank you for the responses. A special thanks to Cristian Ciupitu for pointing out the simple thing that I was missing. I'm sure all of the answers would have worked if I knew enough to use them, but I am a programming noob who has just started learning VB.
I'm very glad I found this community.....hopefully someday I will be able to give more than I take. :lol
BlueBeast
2010-09-27 02:28:16