how to show zero cent in amount in words when there is .00 in decimal value
Public Function AmountInWords(ByVal nAmount As String, Optional ByVal wAmount _ As String = vbNullString, Optional ByVal nSet As Object = Nothing) As String 'Let's make sure entered value is numeric If Not IsNumeric(nAmount) Then Return "Please enter numeric values only."
Dim tempDecValue As String = String.Empty : If InStr(nAmount, ".") Then _
tempDecValue = nAmount.Substring(nAmount.IndexOf("."))
nAmount = Replace(nAmount, tempDecValue, String.Empty)
Try
Dim intAmount As Long = nAmount
If intAmount > 0 Then
nSet = IIf((intAmount.ToString.Trim.Length / 3) _
> (CLng(intAmount.ToString.Trim.Length / 3)), _
CLng(intAmount.ToString.Trim.Length / 3) + 1, _
CLng(intAmount.ToString.Trim.Length / 3))
Dim eAmount As Long = Microsoft.VisualBasic.Left(intAmount.ToString.Trim, _
(intAmount.ToString.Trim.Length - ((nSet - 1) * 3)))
Dim multiplier As Long = 10 ^ (((nSet - 1) * 3))
Dim Ones() As String = _
{"", "One", "Two", "Three", _
"Four", "Five", _
"Six", "Seven", "Eight", "Nine"}
Dim Teens() As String = {"", _
"Eleven", "Twelve", "Thirteen", _
"Fourteen", "Fifteen", _
"Sixteen", "Seventeen", "Eighteen", "Nineteen"}
Dim Tens() As String = {"", "Ten", _
"Twenty", "Thirty", _
"Forty", "Fifty", "Sixty", _
"Seventy", "Eighty", "Ninety"}
Dim HMBT() As String = {"", "", _
"Thousand", "Million", _
"Billion", "Trillion", _
"Quadrillion", "Quintillion"}
intAmount = eAmount
Dim nHundred As Integer = intAmount 100 : intAmount = intAmount Mod 100
Dim nTen As Integer = intAmount 10 : intAmount = intAmount Mod 10
Dim nOne As Integer = intAmount 1
If nHundred > 0 Then wAmount = wAmount & _
Ones(nHundred) & " Hundred " 'This is for hundreds
If nTen > 0 Then 'This is for tens and teens
If nTen = 1 And nOne > 0 Then 'This is for teens
wAmount = wAmount & Teens(nOne) & " "
Else 'This is for tens, 10 to 90
wAmount = wAmount & Tens(nTen) & IIf(nOne > 0, "-", " ")
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
Else 'This is for ones, 1 to 9
If nOne > 0 Then wAmount = wAmount & Ones(nOne) & " "
End If
wAmount = wAmount & HMBT(nSet) & " "
wAmount = AmountInWords(CStr(CLng(nAmount) - _
(eAmount * multiplier)).Trim & tempDecValue, wAmount, nSet - 1)
Else
If Val(nAmount) = 0 Then nAmount = nAmount & _
tempDecValue : tempDecValue = String.Empty
If (Math.Round(Val(nAmount), 2) * 100) > 0 Then wAmount = _
Trim(AmountInWords(CStr(Math.Round(Val(nAmount), 2) * 100), _
wAmount.Trim & " And ", 1)) & " Cents"
End If
Catch ex As Exception
' MessageBox.Show("Error Encountered: " & ex.Message, _ ' "Convert Numbers To Words", _ ' MessageBoxButtons.OK, MessageBoxIcon.Error) ' Return "!#ERROR_ENCOUNTERED" End Try
'Trap null values
If IsNothing(wAmount) = True Then wAmount = String.Empty Else wAmount = _
IIf(InStr(wAmount.Trim.ToLower, ""), _
wAmount.Trim, wAmount.Trim & " ")
'Display the result
Return wAmount
End Function
this is my code but when i got .00 in decimal place i want that it should print zero cents
You check if nAmount > 0
, but you do not check if nAmount = 0
.
If nAmount = 0 then wAnount = "Zero"
Because you are looking at dollars and cents - these are two separate numbers concatenated into a single string for human readability purposes. So, instead of trying to write the entire lot in one function, call it twice, once for the dollar part, and then once for the cents part. This gives you the flexibility to provide an answer of "Zero Dollars and Zero Cents". This will also give you more efficient code in the function.
You also don't check if nAmount < 0
. What do you do then?
上一篇: 分析Java Web Start应用程序