Vb.net 将十进制值转换为单词?
嗨,伙计们,我只是想知道你们是否能帮我做节目 这是关于“数字到单词的转换”,我找到了一个程序,但问题是我需要显示数字中的小数点,所以我如何将小数点转换为单词 例150.25=一百五十二五美分Vb.net 将十进制值转换为单词?,vb.net,visual-studio-2010,decimal-point,decimalformat,Vb.net,Visual Studio 2010,Decimal Point,Decimalformat,嗨,伙计们,我只是想知道你们是否能帮我做节目 这是关于“数字到单词的转换”,我找到了一个程序,但问题是我需要显示数字中的小数点,所以我如何将小数点转换为单词 例150.25=一百五十二五美分 我正在使用Visual Studio 2010以下是我提出的快速谷歌搜索: 看来我能胜任这项工作 Private Function ConvertNumberToENG(ByVal amount As String) As String Dim dollars, cents, temp D
我正在使用Visual Studio 2010以下是我提出的快速谷歌搜索: 看来我能胜任这项工作
Private Function ConvertNumberToENG(ByVal amount As String) As String
Dim dollars, cents, temp
Dim decimalPlace, count
Dim place(9) As String
place(2) = " Thousand "
place(3) = " Million "
place(4) = " Billion "
place(5) = " Trillion "
' String representation of amount.
amount = amount.Trim()
amount = amount.Replace(",", "")
' Position of decimal place 0 if none.
decimalPlace = amount.IndexOf(".")
' Convert cents and set string amount to dollar amount.
If decimalPlace > 0 Then
cents = GetTens(amount.Substring(decimalPlace + 1).PadRight(2, "0").Substring(0, 2))
amount = amount.Substring(0, decimalPlace).Trim()
End If
count = 1
Do While amount <> ""
temp = GetHundreds(amount.Substring(Math.Max(amount.Length, 3) - 3))
If temp <> "" Then dollars = temp & place(count) & dollars
If amount.Length > 3 Then
amount = amount.Substring(0, amount.Length - 3)
Else
amount = ""
End If
count = count + 1
Loop
Select Case dollars
Case ""
dollars = "No Dollars"
Case "One"
dollars = "One Dollar"
Case Else
dollars = dollars & " Dollars"
End Select
Select Case cents
Case ""
cents = " and No Cents"
Case "One"
cents = " and One Cent"
Case Else
cents = " and " & cents & " Cents"
End Select
ConvertNumberToENG = dollars & cents
End Function
' Converts a number from 100-999 into text
Function GetHundreds(ByVal amount As String) As String
Dim Result As String
If Not Integer.Parse(amount) = 0 Then
amount = amount.PadLeft(3, "0")
' Convert the hundreds place.
If amount.Substring(0, 1) <> "0" Then
Result = GetDigit(amount.Substring(0, 1)) & " Hundred "
End If
' Convert the tens and ones place.
If amount.Substring(1, 1) <> "0" Then
Result = Result & GetTens(amount.Substring(1))
Else
Result = Result & GetDigit(amount.Substring(2))
End If
GetHundreds = Result
End If
End Function
' Converts a number from 10 to 99 into text.
Private Function GetTens(ByRef TensText As String) As String
Dim Result As String
Result = "" ' Null out the temporary function value.
If TensText.StartsWith("1") Then ' If value between 10-19...
Select Case Integer.Parse(TensText)
Case 10 : Result = "Ten"
Case 11 : Result = "Eleven"
Case 12 : Result = "Twelve"
Case 13 : Result = "Thirteen"
Case 14 : Result = "Fourteen"
Case 15 : Result = "Fifteen"
Case 16 : Result = "Sixteen"
Case 17 : Result = "Seventeen"
Case 18 : Result = "Eighteen"
Case 19 : Result = "Nineteen"
Case Else
End Select
Else ' If value between 20-99...
Select Case Integer.Parse(TensText.Substring(0, 1))
Case 2 : Result = "Twenty "
Case 3 : Result = "Thirty "
Case 4 : Result = "Forty "
Case 5 : Result = "Fifty "
Case 6 : Result = "Sixty "
Case 7 : Result = "Seventy "
Case 8 : Result = "Eighty "
Case 9 : Result = "Ninety "
Case Else
End Select
Result = Result & GetDigit(TensText.Substring(1, 1)) ' Retrieve ones place.
End If
GetTens = Result
End Function
' Converts a number from 1 to 9 into text.
Private Function GetDigit(ByRef Digit As String) As String
Select Case Integer.Parse(Digit)
Case 1 : GetDigit = "One"
Case 2 : GetDigit = "Two"
Case 3 : GetDigit = "Three"
Case 4 : GetDigit = "Four"
Case 5 : GetDigit = "Five"
Case 6 : GetDigit = "Six"
Case 7 : GetDigit = "Seven"
Case 8 : GetDigit = "Eight"
Case 9 : GetDigit = "Nine"
Case Else : GetDigit = ""
End Select
End Function
专用函数ConvertNumberToENG(ByVal amount作为字符串)作为字符串
暗淡的美元、美分、临时工
昏暗的地方,伯爵
调暗位置(9)为字符串
排名(2)=“千”
排名(3)=“百万”
排名(4)=“十亿”
排名(5)=“万亿”
'金额的字符串表示形式。
amount=amount.Trim()
金额=金额。替换(“,”,“”)
'如果没有,小数点后0位的位置。
decimalPlace=金额指数(“.”)
'转换美分并将字符串金额设置为美元金额。
如果小数点>0,则
仙=GetTens(amount.Substring(decimalPlace+1).PadRight(2,“0”).Substring(0,2))
amount=amount.Substring(0,小数点位置).Trim()
如果结束
计数=1
当金额为“”时执行此操作
temp=get数百(amount.Substring(数学最大值(amount.Length,3)-3))
如果为“临时”,则美元=临时和地点(计数)和美元
如果amount.Length>3,则
amount=amount.Substring(0,amount.Length-3)
其他的
amount=“”
如果结束
计数=计数+1
环
选择案例美元
案例“”
美元=“没有美元”
案例“一”
美元=“一美元”
其他情况
美元=美元和“美元”
结束选择
选择每箱分
案例“”
美分=“没有美分”
案例“一”
美分=“和一美分”
其他情况
美分=“和”&美分&“美分”
结束选择
ConvertNumberToENG=美元和美分
端函数
'将数字从100-999转换为文本
函数get数百(ByVal amount作为字符串)作为字符串
将结果变暗为字符串
如果不是整数。解析(金额)=0,则
金额=金额。左键(3,“0”)
“把几百个地方都换了。
如果amount.Substring(0,1)“0”,则
结果=GetDigit(金额.子字符串(0,1))和“百”
如果结束
'转换十和一的位置。
如果amount.Substring(1,1)“0”,则
结果=结果和获取(数量子字符串(1))
其他的
结果=结果和GetDigit(金额子字符串(2))
如果结束
Get=结果
如果结束
端函数
'将10到99之间的数字转换为文本。
私有函数GetTens(ByRef TensText作为字符串)作为字符串
将结果变暗为字符串
Result=“”清空临时函数值。
如果TensText.StartsWith(“1”),则“如果值介于10-19之间。。。
选择Case Integer.Parse(文本)
案例10:Result=“十”
案例11:Result=“十一”
案例12:Result=“十二”
案例13:Result=“十三”
案例14:Result=“十四”
案例15:Result=“十五”
案例16:Result=“十六”
案例17:结果=“十七”
案例18:Result=“十八”
案例19:结果=“十九”
其他情况
结束选择
Else'如果值介于20-99之间。。。
选择Case Integer.Parse(TensText.Substring(0,1))
案例2:结果=“二十”
案例3:结果=“三十”
案例4:结果=“四十”
案例5:结果=“五十”
案例6:结果=“六十”
案例7:结果=“70”
案例8:Result=“80”
案例9:结果=“90”
其他情况
结束选择
Result=Result&GetDigit(TensText.Substring(1,1))'检索一个位置。
如果结束
GetTens=结果
端函数
'将从1到9的数字转换为文本。
私有函数GetDigit(ByRef Digit作为字符串)作为字符串
选择大小写整数。解析(数字)
案例1:GetDigit=“一”
案例2:GetDigit=“两个”
案例3:GetDigit=“三”
案例4:GetDigit=“四”
案例5:GetDigit=“五”
案例6:GetDigit=“六”
案例7:GetDigit=“七”
案例8:GetDigit=“八”
案例9:GetDigit=“九”
其他情况:GetDigit=“”
结束选择
端函数
因此,如果您已经有了一个可以转换整数的程序,那么您可以轻松地将其用于小数
Dim d As Decimal = 150.25D
Dim arr = d.ToString(CultureInfo.InvariantCulture).Split("."C)
Dim phrase = runExistingProgram(Integer.Parse(arr(0))) + " dollars"
If arr.Length > 1 AndAlso arr(1) <> "0" Then
phrase += " and " + runExistingProgram(Integer.Parse(arr(1))) + " cents"
End If
尺寸d为十进制=150.25D
Dim arr=d.ToString(CultureInfo.InvariantCulture).Split(“.”C)
Dim phrase=runExistingProgram(Integer.Parse(arr(0))+“美元”
如果arr.Length>1且arr(1)“0”,则
短语+=”和“+runExistingProgram(Integer.Parse(arr(1)))+”美分”
如果结束
公共类ConverttoWord
Public Shared Function ConvertNum(ByVal Input As Decimal) As String
Dim formatnumber As String
Dim numparts(10) As String ' break the number into parts
Dim suffix(10) As String 'trillion, billion .million etc
Dim Wordparts(10) As String 'add the number parts and suffix
Dim output As String = Nothing
Dim T, B, M, TH, H, C As String
formatnumber = Format(Input, "0000000000000.00") 'format the input number to a 16 characters string by suffixing and prefixing 0s
'
numparts(0) = primWord(Mid(formatnumber, 1, 1)) 'Trillion
numparts(1) = primWord(Mid(formatnumber, 2, 1)) 'hundred billion..x
numparts(2) = primWord(Mid(formatnumber, 3, 2)) 'billion
numparts(3) = primWord(Mid(formatnumber, 5, 1)) 'hundred million...x
numparts(4) = primWord(Mid(formatnumber, 6, 2)) 'million
numparts(5) = primWord(Mid(formatnumber, 8, 1)) 'hundred thousand....x
numparts(6) = primWord(Mid(formatnumber, 9, 2)) 'thousand
numparts(7) = primWord(Mid(formatnumber, 11, 1)) 'hundred
numparts(8) = primWord(Mid(formatnumber, 12, 2)) 'Tens
numparts(9) = primWord(Mid(formatnumber, 15, 2)) 'cents
suffix(0) = " Trillion "
suffix(1) = " Hundred " '....x
suffix(2) = " Billion "
suffix(3) = " Hundred " ' ....x
suffix(4) = " Million "
suffix(5) = " Hundred " ' .....x
suffix(6) = " Thousand "
suffix(7) = " Hundred "
suffix(8) = " "
suffix(9) = ""
For i = 0 To 9
If numparts(i) <> "" Then
Wordparts(i) = numparts(i) & suffix(i)
End If
T = Wordparts(0)
If Wordparts(1) <> "" And Wordparts(2) = "" Then
B = Wordparts(1) & " Billion "
Else
B = Wordparts(1) & Wordparts(2)
End If
If Wordparts(3) <> "" And Wordparts(4) = "" Then
M = Wordparts(3) & " Million "
Else
M = Wordparts(3) & Wordparts(4)
End If
If Wordparts(5) <> "" And Wordparts(6) = "" Then
TH = Wordparts(5) & " Thousand "
Else
TH = Wordparts(5) & Wordparts(6)
End If
H = Wordparts(7) & Wordparts(8)
If Wordparts(9) = "" Then
C = " and Zero Cents "
Else
C = " and " & Wordparts(9) & " Cents "
End If
Next
output = T & B & M & TH & H & C
Return output
End Function
Public Shared Function primWord(ByVal Num As Integer) As String
'This two dimensional array store the primary word convertion of numbers 0 to 99
primWord = ""
Dim wordList(,) As Object = {{1, "One"}, {2, "Two"}, {3, "Three"}, {4, "Four"}, {5, "Five"},
{6, "Six "}, {7, "Seven "}, {8, "Eight "}, {9, "Nine "}, {10, "Ten "}, {11, "Eleven "}, {12, "Twelve "}, {13, "Thirteen "},
{14, "Fourteen "}, {15, "Fifteen "}, {16, "Sixteen "}, {17, "Seventeen "}, {18, "Eighteen "}, {19, "Nineteen "},
{20, "Twenty "}, {21, "Twenty One "}, {22, "Twenty Two"}, {23, "Twenty Three"}, {24, "Twenty Four"}, {25, "Twenty Five"},
{26, "Twenty Six"}, {27, "Twenty Seven"}, {28, "Twenty Eight"}, {29, "Twenty Nine"}, {30, "Thirty "}, {31, "Thirty One "},
{32, "Thirty Two"}, {33, "Thirty Three"}, {34, "Thirty Four"}, {35, "Thirty Five"}, {36, "Thirty Six"}, {37, "Thirty Seven"},
{38, "Thirty Eight"}, {39, "Thirty Nine"}, {40, "Forty "}, {41, "Forty One "}, {42, "Forty Two"}, {43, "Forty Three"},
{44, "Forty Four"}, {45, "Forty Five"}, {46, "Forty Six"}, {47, "Forty Seven"}, {48, "Forty Eight"}, {49, "Forty Nine"},
{50, "Fifty "}, {51, "Fifty One "}, {52, "Fifty Two"}, {53, "Fifty Three"}, {54, "Fifty Four"}, {55, "Fifty Five"},
{56, "Fifty Six"}, {57, "Fifty Seven"}, {58, "Fifty Eight"}, {59, "Fifty Nine"}, {60, "Sixty "}, {61, "Sixty One "},
{62, "Sixty Two"}, {63, "Sixty Three"}, {64, "Sixty Four"}, {65, "Sixty Five"}, {66, "Sixty Six"}, {67, "Sixty Seven"}, {68, "Sixty Eight"},
{69, "Sixty Nine"}, {70, "Seventy "}, {71, "Seventy One "}, {72, "Seventy Two"}, {73, "Seventy Three"}, {74, "Seventy Four"},
{75, "Seventy Five"}, {76, "Seventy Six"}, {77, "Seventy Seven"}, {78, "Seventy Eight"}, {79, "Seventy Nine"},
{80, "Eighty "}, {81, "Eighty One "}, {82, "Eighty Two"}, {83, "Eighty Three"}, {84, "Eighty Four"}, {85, "Eighty Five"},
{86, "Eighty Six"}, {87, "Eighty Seven"}, {88, "Eighty Eight"}, {89, "Eighty Nine"}, {90, "Ninety "}, {91, "Ninety One "},
{92, "Ninety Two"}, {93, "Ninety Three"}, {94, "Ninety Four"}, {95, "Ninety Five"}, {96, "Ninety Six"}, {97, "Ninety Seven"},
{98, "Ninety Eight"}, {99, "Ninety Nine"}}
Dim i As Integer
For i = 0 To UBound(wordList)
If Num = wordList(i, 0) Then
primWord = wordList(i, 1)
Exit For
End If
Next
Return primWord
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBoxWord.Text = ConverttoWord.ConvertNum(TextBoxfigure.Text)
End Sub
你发现了一个只翻译整数的程序吗?你能看一看吗?我对vb还是新手,我们的老师只是让我们做这个转换,而没有实际解释关于这个程序的任何内容,所以不管怎样,学习tnx这个东西很难获得建议^^
enter code here