any suggestions on a currency breakdown

EPYK

Member
Joined
Jan 14, 2007
Messages
12
Programming Experience
1-3
my program takes an amount of money and divides it up into the most efficient denomination of bills...

main algorithm:
loop
bill amount = integer(money / particular bill)
money = money MOD particular bill
end loop


heres what i got so far

VB.NET:
Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click
        Dim strMoney As String
        Dim dblMoney As Double
        Dim dblTotal As Double
        Dim arrDivisor() As Double = {1000.0, 100.0, 50.0, 20.0, 10.0, 5.0, 1.0, 0.25, 0.1, 0.05, 0.01}
        Dim arrChangeType() As String = {"thousand dollar bills: ", "hundred dollar bills: ", "fifty dollar bills: ", "twenty dollar bills: ", "ten dollar bills: ", "five dollar bills: ", "one dollar bills: ", "quarters: ", "dimes: ", "nickels: ", "pennies: "}
        Dim arrChangeAmounts(11) As Integer


        Dim intCounter As Integer

        strMoney = txtMoney.Text

        If StringCheck(strMoney, "cur") = True Then
            MsgBox("You have entered a bad numeric amount.", , "ERROR")
            txtMoney.Clear()
        Else

            dblMoney = CDbl(strMoney)

            For intCounter = 0 To 10   ' Set up 11 repetitions.

                If dblMoney > arrDivisor(intCounter) Or dblMoney = arrDivisor(intCounter) Then

                    'division, result is a double. I.E. 1.234  gets converted to integer and drops the decimal portion
                    arrChangeAmounts(intCounter) = CInt(dblMoney / arrDivisor(intCounter))

                    'updates the label array with a bill/change type and ammount of that type
                    LabelArray(intCounter + 1).Text = arrChangeType(intCounter) & arrChangeAmounts(intCounter)

                    'the adds all the change made and puts it in a disabled textbox
                    dblTotal = dblTotal + (arrChangeAmounts(intCounter) * arrDivisor(intCounter))

                    ' i made my own modulus function to get over the improper division
                    dblMoney = dblMoney mod arrDivisor(intCounter))

                    ' this tries to fix the problem with MS's version of division 
                    dblMoney = dblMoney + 0.00001

                Else
                    LabelArray(intCounter + 1).Text = arrChangeType(intCounter) & "0"
                End If

            Next intCounter   ' Increment intCounter.

            txtTotal.Text = CStr(dblTotal)

        End If

    End Sub
 
try this code, did at least give correct result with amount 1001,01 ;)
VB.NET:
Sub exchange(ByVal amount As Double)
    [COLOR=darkgreen]'arrays[/COLOR]
    Dim arrDivisor() As Double = {1000, 100, 50, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01}
    Dim arrChangeType() As String = {"thousand dollar bills: ", "hundred dollar bills: ", "fifty dollar bills: ", "twenty dollar bills: ", _
"ten dollar bills: ", "five dollar bills: ", "one dollar bills: ", "quarters: ", "dimes: ", "nickels: ", "pennies: "}
    Dim arrChangeAmounts(10) As Integer
    [COLOR=darkgreen]'calculate[/COLOR]
    For i As Integer = 0 To arrDivisor.Length - 1
        arrChangeAmounts(i) = [SIZE=2]Math.Floor[/SIZE](Math.Round(amount, 2) / arrDivisor(i))
        amount -= (arrChangeAmounts(i) * arrDivisor(i))
    Next
    [COLOR=darkgreen]'present the results[/COLOR]
    Dim sb As New System.Text.StringBuilder
    For i As Integer = 0 To arrDivisor.Length - 1
        sb.AppendLine(arrChangeType(i) & arrChangeAmounts(i))
    Next
    MsgBox(sb.ToString)
End Sub
 
I added the necessary rounding to fix pennies right after I posted.. when I exchange 12,34 I get:
thousand dollar bills: 0
hundred dollar bills: 0
fifty dollar bills: 0
twenty dollar bills: 0
ten dollar bills: 1
five dollar bills: 0
one dollar bills: 2
quarters: 1
dimes: 0
nickels: 1
pennies: 4
 
thankyou

i used a little of both our brains to get this to work..

it has a loop to add info from .01 to 3000 going by .01
no errors
VB.NET:
 Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click
        Dim strMoney As String
        Dim decMoney As Decimal
        Dim decTotal As Decimal
        Dim arrDivisor() As Decimal = {1000.0, 100.0, 50.0, 20.0, 10.0, 5.0, 1.0, 0.25, 0.1, 0.05, 0.01}
        Dim arrChangeType() As String = {"thousand dollar bills: ", "hundred dollar bills: ", "fifty dollar bills: ", "twenty dollar bills: ", "ten dollar bills: ", "five dollar bills: ", "one dollar bills: ", "quarters: ", "dimes: ", "nickels: ", "pennies: "}
        Dim arrChangeAmounts(11) As Integer
        Dim intCounter As Integer
        txtTotal.Text = "0"



        Dim intError As Integer



        strMoney = txtMoney.Text
        'check string for datatype
        If StringCheck(strMoney, "dec") = True Then
            MsgBox("You have entered a bad numeric amount.", , "ERROR")
            txtMoney.Clear()
        Else

            decMoney = CDec(strMoney)


            ''''

            Dim dblX As Decimal
            For dblX = 0.01 To 3000.00 Step 0.01
                decMoney = (dblX)
                txtMoney.Text = CStr(dblX)
                txtTotal.Text = "0"
                '''''


                For intCounter = 0 To 10

                    If decMoney >= arrDivisor(intCounter) Then


                        arrChangeAmounts(intCounter) = Math.Floor(decMoney / arrDivisor(intCounter))

                        LabelArray(intCounter + 1).Text = arrChangeType(intCounter) & CStr(arrChangeAmounts(intCounter))

                        decMoney = decMoney Mod arrDivisor(intCounter)

                        txtTotal.Text = CStr(CDec(txtTotal.Text) + (arrDivisor(intCounter) * arrChangeAmounts(intCounter)))

                    Else
                        LabelArray(intCounter + 1).Text = CStr(arrChangeType(intCounter)) & "0"
                    End If


                Next
                If CDec(txtTotal.Text) <> CDec(txtMoney.Text) Then
                    intError += 1
                    MsgBox(dblX)
                End If
            Next


        End If


        ''''
        MsgBox(CStr(intError) & " errors")

    End Sub

    Private Function StringCheck(ByVal strTestString As String, ByVal strType As String) As Boolean
        Dim blnError As Boolean = False

        strType.ToLower()

        If strType = "integer" Or strType = "int" Then

            If IsNumeric(strTestString) = False Then
                blnError = True
            End If

        ElseIf strType = "Decimal" Or strType = "dec" Then

            If IsNumeric(strTestString) = False Then
                blnError = True
            End If

        ElseIf strType = "boolean" Or strType = "bln" Or strType = "bool" Then
            If strTestString <> "true" And strTestString <> "false" Then
                blnError = True
            End If

        End If

        Return blnError
    End Function


    'declare an array of labels
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SetControlArray()
    End Sub

    'this points the labelarray towards actual labels on the form
    Sub SetControlArray()
        LabelArray(1) = Label1
        LabelArray(2) = Label2
        LabelArray(3) = Label3
        LabelArray(4) = Label4
        LabelArray(5) = Label5
        LabelArray(6) = Label6
        LabelArray(7) = Label7
        LabelArray(8) = Label8
        LabelArray(9) = Label9
        LabelArray(10) = Label10
        LabelArray(11) = Label11

    End Sub
    Dim LabelArray(11) As Label
 
I see no problems with the code in post 2...?
6 lines of code is better than 60 y'know.
 
yeah i know. but im nbot supposed to know that stuff yet..
and id rather not get to advanced right now. just so i can look back to it in a week and still know what it does...
im only a simple VBer right now.. i do have a class in learning vb under ma belt though...
 
Back
Top