以下是一个把数值转换成英文币值的表示方法,(从一个vbs参照而来)已解决
本帖最后由 scorpio1102 于 2012-6-19 11:19 编辑从一个vb程序翻译过来的,应该翻译的有点错误,请哪位大哥排下错,先谢了.
opt('MustDeclareVars',1)
;Func SpellNumber(ByVal $MyNumber)
Func SpellNumber($MyNumber)
Dim $Dollars, $Cents, $Temp
Dim $DecimalPlace, $Count
ReDim $Place(9) as string
$Place(2) = " Thousand "
$Place(3) = " Million "
$Place(4) = " Billion "
$Place(5) = " Trillion "
; String representation of amount.
$MyNumber = stringstripws(String($MyNumber),3)
; Position of decimal place 0 if none.
$DecimalPlace = stringInStr($MyNumber, ".")
; Convert cents and set MyNumber to dollar amount.
If $DecimalPlace > 0 Then
$Cents = GetTens(stringLeft(stringMid($MyNumber, $DecimalPlace + 1) & "00", 2))
$MyNumber = stringstripws(stringLeft($MyNumber, $DecimalPlace - 1))
EndIf
$Count = 1
While $MyNumber <> ""
$Temp = GetHundreds(stringRight($MyNumber, 3))
If $Temp <> "" Then $Dollars = $Temp & $Place($Count) & $Dollars
If stringLen($MyNumber) > 3 Then
$MyNumber = stringLeft($MyNumber, stringLen($MyNumber) - 3)
Else
$MyNumber = ""
EndIf
$Count = $Count + 1
continueLoop
Select Case $Dollars
Case ""
$Dollars = "No Dollars"
Case "One"
$Dollars = "One Dollar"
Case Else
$Dollars = $Dollars & " Dollars"
EndSelect
Select Case $Cents
Case ""
$Cents = " and No Cents"
Case "One"
$Cents = " and One Cent"
Case Else
$Cents = " and " & $Cents & " Cents"
EndSelect
$SpellNumber = $Dollars & $Cents
EndFunc
;Converts a number from 100-999 into text
;Func GetHundreds(ByVal MyNumber)
Func GetHundreds($MyNumber)
;Dim $Result As String
Dim $Result as string
If Number($MyNumber) = 0 Then Exit Func
$MyNumber = stringRight("000" & $MyNumber, 3)
; Convert the hundreds place.
If stringMid($MyNumber, 1, 1) <> "0" Then
$Result = GetDigit(stringMid($MyNumber, 1, 1)) & " Hundred "
EndIf
; Convert the tens and ones place.
If stringMid($MyNumber, 2, 1) <> "0" Then
$Result = $Result & GetTens(stringMid($MyNumber, 2))
Else
$Result = $Result & GetDigit(stringMid($MyNumber, 3))
EndIf
$GetHundreds = $Result
EndFunc
; Converts a number from 10 to 99 into text.
Func GetTens($TensText)
;Dim $Result As String
Dim $Result as string
$Result = "" ; Null out the temporary function value.
If number(stringLeft($TensText, 1)) = 1 Then ; If value between 10-19...
Select Case number($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
EndSelect
Else ; If value between 20-99...
Select Case number(stringLeft($TensText, 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
EndSelect
; Retrieve ones place.
$Result = $Result & $GetDigit(stringright($TensText, 1))
EndIf
$GetTens = $Result
EndFunc
; Converts a number from 1 to 9 into text.
Func GetDigit($Digit)
Select Case number($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 = ""
EndSelect
EndFunc http://www.autoitx.com/forum.php?mod=redirect&goto=findpost&ptid=30606&pid=403103&fromuid=786 回复 1# scorpio1102
楼主的AU3语法基础有待提高呀!
没事做按你给出的代码,自己琢磨了一个,你自己再扩展吧!
Local $Number = '55,778.20'
MsgBox(0, $Number, _Convertor($Number))
MsgBox(0, '2144578788778.156', _Convertor('2144578788778.156'))
Func _Convertor($_number)
; 如果需要换算更大的数值, 请将其单位按顺序加入数组,并修改数组下标!!
Local $Place=['', " Thousand ", " Million ", " Billion ", " Trillion "]
$_number= StringStripWS($_number, 8)
; 检测非法数值
If StringRegExp($_number, '[^\.,\d]|,,|\.\.|\d+\.\d+\.\d') Or StringRegExp($_number, '^\D|\D$') Then Return SetError(1)
Local $cents = '', $Result = ''
$_number = StringReplace($_number, ',', '')
If StringInStr($_number, '.') Then
$_number = Round($_number, 2)
$cents = StringLeft(StringRegExpReplace($_number, '\d+\.(\d+)', '\1') & '0', 2)
$cents = GetHundreds($cents) & ' Cents'
$_number = StringRegExpReplace($_number, '([^\.]+)\.\d+', '\1')
EndIf
Local $i = 0
While 1
$n = StringRight($_number, 3)
$Result = GetHundreds($n) & $Place[$i] & $Result
$_number = StringTrimRight($_number, 3)
If $_number = '' Then ExitLoop
$i += 1
WEnd
Return StringRegExpReplace($Result & ' ' & $cents, '\h+', ' ')
EndFunc
;以百位数 + 十位数 + 个位数: 100-999 转换成英文
Func GetHundreds($MyNumber)
Local $h, $t, $D
If Number($MyNumber) = 0 Then Return ''
$MyNumber = StringFormat('%03d', $MyNumber)
; 确定百位数的值
$h = StringLeft($MyNumber, 1)
If $h <> "0" Then
$h = GetDigit($h) & " Hundred "
Else
$h = ''
EndIf
; 十位数 及 个位数
$t = StringMid($MyNumber, 2, 1)
$d = StringRight($MyNumber, 1)
If $t = 1 Then
$t = GetTens(StringRight($MyNumber, 2))
ElseIf $t = 0 Then
$t = GetDigit($d)
Else
$t = GetTens(StringRight($MyNumber, 2)) & ' ' & GetDigit($d)
EndIf
Return $h & $t
EndFunc ;==>GetHundreds
; 转换十位的数字 10 到 90 成英文.
Func GetTens($TensText)
Local $Result
If StringLeft($TensText, 1) = 0 Then
Return ''
ElseIf StringLeft($TensText, 1) = 1 Then ; 10 ~ 19
Switch Number($TensText)
Case 10
Return "Ten"
Case 11
Return "Eleven"
Case 12
Return "Twelve"
Case 13
Return "Thirteen"
Case 14
Return "Fourteen"
Case 15
Return "Fifteen"
Case 16
Return "Sixteen"
Case 17
Return "Seventeen"
Case 18
Return "Eighteen"
Case 19
Return "Nineteen"
EndSwitch
Else; 20~99
Switch Number(StringLeft($TensText, 1))
Case 2
Return "Twenty"
Case 3
Return "Thirty"
Case 4
Return "Forty"
Case 5
Return "Fifty"
Case 6
Return "Sixty"
Case 7
Return "Seventy"
Case 8
Return "Eighty"
Case 9
Return "Ninety"
EndSwitch
EndIf
EndFunc ;==>GetTens
; 个位的数字 1 to 9
Func GetDigit($Digit)
Switch $Digit
Case 1
Return "One"
Case 2
Return "Two"
Case 3
Return "Three"
Case 4
Return "Four"
Case 5
Return "Five"
Case 6
Return "Six"
Case 7
Return "Seven"
Case 8
Return "Eight"
Case 9
Return "Nine"
Case Else
Return ""
EndSwitch
EndFunc ;==>GetDigit 回复 3# user3000
我以为没人回了呢,谢了,之后又看了autoit help 发现真的语法错误很多呢,呵呵.您帮了我很大的忙呀,好心人.
页:
[1]