当十进制值为.00时,如何在文字中显示零分
公共函数AmountInWords(ByVal nAmount As String,可选ByVal wAmount _ As String = vbNullString,可选ByVal nSet As Object = Nothing)作为字符串'让我们确保输入的值是数字如果不是IsNumeric(nAmount)然后返回“请仅输入数字值“。
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,_'“将数字转换为单词”,_'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
结束功能
这是我的代码,但是当我在小数点后得到.00时,我希望它应该打印零分
您检查nAmount > 0
,但不检查nAmount = 0
。
If nAmount = 0 then wAnount = "Zero"
因为您正在查看美元和美分 - 为了便于人们阅读,这些是两个单独的字符串连接成一个字符串。 所以,不要试图在一个函数中写出整个批次,而应该调用它两次,一次为美元部分,然后一次为美分部分。 这使您可以灵活地提供“零美元和零美分”的答案。 这也将为您提供更高效的代码。
您也不检查nAmount < 0
。 那你怎么办呢?
上一篇: how to show zero cent in amount in words when there is .00 in decimal value