Module HomeWork_4_2
Public Shared Function MoneyDistribution(ByVal argMoney As Double) As String
Dim retStr As String = String.Empty
Dim CountType() As Double = New Double() _
{1000 ,500 ,100 ,50 ,20 ,10 ,5 ,2 ,1 ,.5 ,.25}
Dim calcValue As Double = argMoney
For i As Integer = 0 to CountType.Length -1
Dim totalUnit = System.Convert.ToInt32( calcValue / CountType(i))
If( totalUnit > 0 ) Then
calcValue = calcValue - ( totalUnit * CountType(i))
Dim moneyType As String = If((CountType(i) > 10), "billie", "coin")
retStr = string.Format( "{0}Num. of {1} {2} baht(s) = {3}" & vbCr _
,retStr ,moneyType ,CountType(i).ToString("#,##0") ,totalUnit.ToString("#,##0"))
End If
Next
Return retStr
End Function
End Module
Sub Main
Console.WriteLine( MoneyDistribution(24900.75) )
End Sub
Public Shared Function MoneyDistribution(ByVal argMoney As Double) As String
....
....
Public Shared Function MoneyDistribution(ByVal argMoney As Double) As String
Dim retStr As String = String.Empty
Dim CountType As Double() = New Double() {1000, 500, 100, 50, 20, 10, _
5, 2, 1, 0.5, 0.25}
Dim calcValue As Double = argMoney
Const cxSpace As Char = " "c
Const cxNewLine As String = vbCrLf
For i As Integer = 0 To CountType.Length - 1
Dim totalUnit As Integer = 0
Dim moneyFlag As Double = CountType(i)
If moneyFlag <= calcValue Then
totalUnit = Int(calcValue / moneyFlag)
End If
If (totalUnit > 0) Then
calcValue = calcValue - (totalUnit * moneyFlag)
Dim moneyType As String _
= String.Format("[{0}]", If((moneyFlag > 10), "billie", "coin").PadLeft(7, cxSpace))
retStr = String.Format("{0}Num. of {1} {2} baht(s) = {3}" & cxNewLine _
, retStr, moneyType _
, moneyFlag.ToString("#,##0.00").PadLeft(8, cxSpace) _
, totalUnit.ToString("#,##0").PadLeft(10, cxSpace))
If calcValue = 0 Then
Exit For
End If
End If
Next
Return retStr
End Function