将人民币小写金额转换成大写的vb代码
- 提问者网友:感性作祟
- 2021-05-31 18:42
- 五星知识达人网友:迟山
- 2021-05-31 19:38
'本人写的代码只是提供一种思路并不完整,但可以实现 几几点几几 这种形式的转换,需要更全面的转换还得你自己研究修改代码,不好意思
Public Sub change(k As Integer)
If k = 1 Then Text2.Text = Text2.Text + "壹"
If k = 2 Then Text2.Text = Text2.Text + "贰"
If k = 3 Then Text2.Text = Text2.Text + "弎"
If k = 4 Then Text2.Text = Text2.Text + "肆"
If k = 5 Then Text2.Text = Text2.Text + "伍"
If k = 6 Then Text2.Text = Text2.Text + "陆"
If k = 7 Then Text2.Text = Text2.Text + "柒"
If k = 8 Then Text2.Text = Text2.Text + "捌"
If k = 9 Then Text2.Text = Text2.Text + "九"
End Sub
Private Sub Command1_Click()
Call change(Val(Mid(Trim(Text1.Text), 1, 1)))
Text2.Text = Text2.Text + "拾"
Call change(Val(Mid(Trim(Text1.Text), 2, 1)))
Text2.Text = Text2.Text + "圆"
Call change(Val(Mid(Trim(Text1.Text), 4, 1)))
Text2.Text = Text2.Text + "角"
Call change(Val(Mid(Trim(Text1.Text), 5, 1)))
Text2.Text = Text2.Text + "分"
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
End Sub
- 1楼网友:千夜
- 2021-05-31 21:18
这个代码是转英文的,先让你理解工作原理,然后再给你中文的
主要工作原理如下:1200.12
把它拆成000,000,001,200.12,用format(1200,”000,000,000,000.00”)来实现
那么你再把它拆成4个数组,分别是000+billion+ 000 + million 001 + thound + 200 + dollar+ 12 +cent
然后调用GetNum3_en来返回对应的英文描述就好了。
GetNum3_en:这个是接受一个3位数的long型变量,返回一个英文的描述,如传入120,则返回one hundred and twenty
它调用到了GetNum2_en和GetNum1_en
GetNum2_en:这个是返回十位的英文描述,如传入2,则返回twenty
GetNum1_en:这个是返回小于二十的英文描述,如传入12,则返回twelve
Option Explicit Private Sub Command1_Click() '0000,000,000,000 MsgBox GetNumber_en(Val(Me.Text1.Text)) End Sub Private Function GetNumber_en(ByVal iNum As Double) As String Dim sNum As String, sNum_En As String, sAll As String, sDecimal As String Dim sStrArray() As String, i As Long sNum = Format(iNum, "000,000,000,000.00") If InStr(sNum, ".") > 0 Then sDecimal = Right(sNum, Len(sNum) - InStr(sNum, ".")) sNum = Left(sNum, InStr(sNum, ".") - 1) End If sStrArray = Split(sNum, ",") sNum_En = GetNum3_en(sStrArray(0)) If Len(sNum_En) > 0 Then sNum_En = sNum_En & " billiion" & IIf(Val(sStrArray(0)) > 1, "s", "") sAll = sNum_En sNum_En = GetNum3_en(sStrArray(1)) If Len(sNum_En) > 0 Then sNum_En = sNum_En & " milliion" & IIf(Val(sStrArray(1)) > 1, "s", "") If Len(sNum_En) > 0 Then sAll = sAll & IIf(Len(sAll) > 0, " and ", "") & sNum_En sNum_En = GetNum3_en(sStrArray(2)) If Len(sNum_En) > 0 Then sNum_En = sNum_En & " thound" & IIf(Val(sStrArray(2)) > 1, "s", "") If Len(sNum_En) > 0 Then sAll = sAll & IIf(Len(sAll) > 0, " and ", "") & sNum_En sNum_En = GetNum3_en(sStrArray(3)) If Len(sNum_En) > 0 Then sAll = sAll & IIf(Len(sAll) > 0, " and ", "") & sNum_En If Len(sAll) > 0 Then sAll = sAll & " Dollar" & IIf(Val(Join(sStrArray, "")) > 1, "s", "") 'decimal If Len(sDecimal) > 0 Then sNum_En = GetNum3_en(sDecimal) If Len(sNum_En) > 0 Then sAll = sAll & IIf(Len(sAll) > 0, " and ", "") & sNum_En & " Cent" & IIf(Val(sDecimal) > 1, "s", "") End If GetNumber_en = sAll End Function Private Function GetNum3_en(ByVal iNum As Long) As String '3 digit Dim sStr As String sStr = Format(Left(iNum, 3), "000") 'make sure it only have 3 digit If Val(Mid(sStr, 1, 1)) > 0 Then 'Hundred GetNum3_en = GetNum1_En(Mid(sStr, 1, 1)) & " Hundred" & IIf(Val(Mid(sStr, 1, 1)) > 1, "s", "") End If If Val(Mid(sStr, 2, 2)) >= 20 Then 'check whether it more than twenty If Val(Mid(sStr, 2, 1)) > 0 Then 'ten GetNum3_en = GetNum3_en & IIf(Len(GetNum3_en) > 0, " And ", "") & GetNum2_En(Mid(sStr, 2, 1)) End If If Val(Mid(sStr, 3, 1)) > 0 Then 'less then 10 GetNum3_en = GetNum3_en & IIf(Len(GetNum3_en) > 0, " and ", "") & GetNum1_En(Mid(sStr, 3, 1)) End If Else If Val(Mid(sStr, 2, 2)) > 0 Then 'less then 10 GetNum3_en = GetNum3_en & IIf(Len(GetNum3_en) > 0, " and ", "") & GetNum1_En(Mid(sStr, 2, 2)) End If End If End Function Private Function GetNum2_En(ByVal iNum As Long) As String Select Case iNum Case 1 GetNum2_En = "Ten" Case 2 GetNum2_En = "Twenty" Case 3 GetNum2_En = "Thirty" Case 4 GetNum2_En = "Forty" Case 5 GetNum2_En = "Fifty" Case 6 GetNum2_En = "Sixty" Case 7 GetNum2_En = "Seventy" Case 8 GetNum2_En = "Eighty" Case 9 GetNum2_En = "Ninety" Case Else ' End Select End Function
Private Function GetNum1_En(ByVal iNum As Long) As String Select Case iNum Case 1 GetNum1_En = "One" Case 2 GetNum1_En = "Two" Case 3 GetNum1_En = "Three" Case 4 GetNum1_En = "Four" Case 5 GetNum1_En = "Five" Case 6 GetNum1_En = "Six" Case 7 GetNum1_En = "Seven" Case 8 GetNum1_En = "Eight" Case 9 GetNum1_En = "Nine" Case 10 GetNum1_En = "Ten" Case 11 GetNum1_En = "Eleven" Case 12 GetNum1_En = "Twelve" Case 13 GetNum1_En = "Thirteen" Case 14 GetNum1_En = "Fourteen" Case 15 GetNum1_En = "Fifteen" Case 16 GetNum1_En = "Sixteen" Case 17 GetNum1_En = "Seventeen" Case 18 GetNum1_En = "Eighteen" Case 19 GetNum1_En = "Nineteen" Case Else ' End Select End Function