How to determine number of coins ?

If I input $1.50 in a textbox, I would like to analyze the input, so
it can be determined like, $1, 2 for 25cents,   or 4 for 25cents, 5 for 10cents.

How to analyze this problem in vb ?
LVL 1
tanc02Asked:
Who is Participating?
 
mcriderConnect With a Mentor Commented:
This type of question was previously asked in http://www1.experts-exchange.com/bin/Q.10245098 

However, I am posting the answer again modified to include dollars...

Here's the complete example:

Add a textbox and a command button to a new form then add the following code.


Run the program and type the currency into the textbox and press the command button....


Cheers!®©



THE CODE:

    Private Type ExactChange
        Dollars As Long
        Quarters As Long
        Dimes As Long
        Nickels As Long
        Pennies As Long
     End Type
    Private Function GetExactChange(cChange As Currency) As ExactChange
        Dim iVal As Long
        Dim lDL As Long
        Dim lQ As Long
        Dim lD As Long
        Dim lN As Long
         
        iVal = CLng(cChange * 100)
        lQ = Fix(iVal / 25): iVal = iVal - (lQ * 25)
        lD = Fix(iVal / 10): iVal = iVal - (lD * 10)
        lN = Fix(iVal / 5): iVal = iVal - (lN * 5)
        lDL = Fix(lQ / 4): lQ = lQ - (lDL * 4)
        GetExactChange.Dollars = lDL
        GetExactChange.Quarters = lQ
        GetExactChange.Dimes = lD
        GetExactChange.Nickels = lN
        GetExactChange.Pennies = iVal
    End Function
    Private Sub Command1_Click()
        With GetExactChange(CCur(Text1.Text))
            MsgBox "Dollars: " & CStr(.Dollars) & vbCrLf & _
                "Quarters: " & CStr(.Quarters) & vbCrLf & _
                "Dimes: " & CStr(.Dimes) & vbCrLf & _
                "Nickels: " & CStr(.Nickels) & vbCrLf & _
                "Pennies: " & CStr(.Pennies)
        End With
    End Sub
0
 
Slightly_ImperfectCommented:
Private Sub Command1_Click()
Dim price_enetered As String
'declared variables
Dim first_value As Integer
Dim second_value As Integer
Dim one_var As String
Dim remainder As Integer

'flagged
Dim flag_set As Integer

For wtab = 1 To Len(Text1.Text)
    Let one_var = Mid(Text1.Text, wtab, 1)
    If one_var = "." Then
      first_value = Mid(Text1.Text, 1, wtab - 1)
      second_value = Mid(Text1.Text, wtab + 1, Len(Text1.Text))
    End If
Next wtab
'divide the first value to see what it will divide into
'there are three 4 notes in my currency
'£5 £10 £20 £50
'The first value
'i have only done this as a value of £10
'you can work the rest out from this
If first_value < 2 Then
     List1.AddItem first_value & " Pound Coin"
Else
   If first_value < 5 Then
      List1.AddItem first_value & " Pound Coins"
   
   
   Else
      If first_value < 10 Then
        remainder = first_value - 5
        If remainder < 2 Then
         List1.AddItem "5 pound note " & remainder & " Pound Coin"
        Else
         List1.AddItem "5 pound note " & remainder & " Pound Coins"
        End If
      End If
   End If
End If
'i have only done the first instance
If second_value > 5 Then
   Let remainder = second_value - 5
   If remainder < 2 Then
      List1.AddItem "5 pence piece and " & remainder & " pence piece"
   Else
      List1.AddItem "5 pence and " & remainder & " penny pieces"
   End If
End If
Let price_entered = Text1.Text



End Sub

i hope this gives you what you need !!! i have only coded the first instances but at least it will give you an idea where to start from :^)

get in touch if you need any help

0
 
tanc02Author Commented:
Slightly_Imperfect  :

How to change your program, so it will fit USA coins, like $1, 50, 25, 10 and 5 cents ?
0
 
Slightly_ImperfectCommented:
Here is the answer you were looking for !!!!

i hope i have got the currency right !!!
:^)

Private Sub Command1_Click()

Dim price_enetered As String
'declared variables
Dim first_value As Integer
Dim second_value As Integer
Dim one_var As String
Dim remainder As Integer
Dim last_value As Integer
'flagged
Dim flag_set As Integer

For wtab = 1 To Len(Text1.Text)
    Let one_var = Mid(Text1.Text, wtab, 1)
    If one_var = "." Then
      first_value = Mid(Text1.Text, 1, wtab - 1)
      second_value = Mid(Text1.Text, wtab + 1, Len(Text1.Text))
    End If
Next wtab
'divide the first value to see what it will divide into
'there are three 4 notes in my currency
'£5 £10 £20 £50
'The first value
'i have only done this as a value of £10
'you can work the rest out from this
If first_value < 2 Then
     List1.AddItem first_value & " Dollar Coin"
Else
   If first_value < 5 Then
      List1.AddItem first_value & " Dollar Coins"
   
   
   Else
      If first_value < 10 Then
        remainder = first_value - 5
        If remainder < 2 Then
         List1.AddItem "5 dollar bill " & remainder & " Dollar Bill"
        Else
         List1.AddItem "5 dollar bill " & remainder & " Dollar Bill"
        End If
      End If
   End If
End If
'i have only done the first instance
   Let remainder = Mid(second_value, 1, 1) - 5
   Let last_value = Mid(second_value, 2, 1)
   If remainder < 2 Then
      List1.AddItem "50 CENTS and " & remainder & last_value & " CENT"
   Else
      List1.AddItem "50 CENTS and " & remainder & last_value & " CENTS"
   End If
Let price_entered = Text1.Text

End Sub

add a command button a listbox and a textbox then paste the code into the command button it should work !!!!

if i got the terminalogy wrong just play about with it at least you have got the principle!!!!

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.