求一函数,将数据翻译成无数字英文 (在线急等)

colee 2003-04-15 08:00:04
求一函数translate(number)
将数据翻译成无数字英文,如55555555将被自动翻译成:
fifty-five million,five hundred and fifty-five thousand,five hundred and fifty-five,
即是translate("55555555")的值
...全文
288 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
colee 2003-04-16
  • 打赏
  • 举报
回复
搞定
<script>
A= "zero^one^two^three^four^five^six^seven^eight^nine^ten^eleven^twelve^thirteen^fourteen^fifteen^sixteen^seventeen^eighteen^nineteen^^twenty^thirty^forty^fifty^sixty^seventy^eighty^ninety^hundred^^thousand^million^billion^trillion^quadrillion^quintillion".split("^");
B = new Array();
for (i=0;i<1000;i++) {
B[i] = i<20?A[i]:i<100?A[19+Math.floor(i/10)]+(i%10==0?"":"-"+B[i%10]):A[Math.floor(i/100)]+" "+A[29]+(i%100==0?"":" and "+B[i%100]);
}
function readNum(real_number) {
text = "";
with (real_number.split(".")[0].split(",").join("")) {
for (i=0;i<Math.ceil(length/3);i++) {
sec = parseInt(substring(length-3*i-3,length-3*i),10);
text = (i==0&&sec>0&&sec<100&&parseInt(substring(0,length-3),10)>0?" and ":"")+(sec==0&&(i>0||toString(10)!=0)?"":B[sec])+(sec==0?"":" "+(typeof A[30+i]=="undefined"?"undefined".fontcolor("red"):A[30+i]))+(i==0||sec==0||(sec>0&&text=="")?"":",".fontcolor("black")+" ")+text;
}
}
if (real_number.indexOf(".")!=-1) {
with (real_number.split(".")[1].split(",").join("")) {
for (i=0;i<Math.ceil(length/3);i++) {
sec = parseInt(substring(length-3*i-3,length-3*i),10);
document.write(sec);
//text += (i==0?(text==""?A[0]:"")+" point ".fontcolor("black"):"")+(sec==0&&i!=0&&!parseInt(substring(i+1),10)>0?"":B[sec]+" ");
text += (i==0?(text==""?A[0]:"")+" and cents ":"")+(sec==0&&(i>0||toString(10)!=0)?"":B[sec])+(sec==0?"":" ")+(i==0||sec==0||(sec>0&&text=="")?"":"");
}
}
}
document.write((text.indexOf("undefined")==-1?"":typeof alert("数字超长,超出部分将无法正确输出!")=="undefined"?"":"")+text);
}
readNum("55555555.55");
</script>


谢谢以上两位。。。。
另欢迎在此贴出用VB写的
晚上来结帖




colee 2003-04-16
  • 打赏
  • 举报
回复

为了不麻烦,小数后就固定为两位小数
(即and cents ××)
colee 2003-04-16
  • 打赏
  • 举报
回复
to tigerwen01(小虎): 有错误

to ahough(了冉):

很妙啊,不过有一点没达意,因为是用来表示钱数。
小数点后面的,如5.55要转成five and cents fifty-five
可以再帮手么?
ahough 2003-04-16
  • 打赏
  • 举报
回复

<script>
A = "zero^one^two^three^four^five^six^seven^eight^nine^ten^eleven^twelve^thirteen^fourteen^fifteen^sixteen^seventeen^eighteen^nineteen^^twenty^thirty^forty^fifty^sixty^seventy^eighty^ninety^hundred^^thousand^million^billion^trillion^quadrillion^quintillion".split("^");
B = new Array();
for (i=0;i<1000;i++) {
B[i] = i<20?A[i]:i<100?A[19+Math.floor(i/10)]+(i%10==0?"":"-"+B[i%10]):A[Math.floor(i/100)]+" "+A[29]+(i%100==0?"":" and "+B[i%100]);
}
function readNum() {
text = "";
with (num2En.arabNum.value.split(".")[0].split(",").join("")) {
for (i=0;i<Math.ceil(length/3);i++) {
sec = parseInt(substring(length-3*i-3,length-3*i),10);
text = (i==0&&sec>0&&sec<100&&parseInt(substring(0,length-3),10)>0?" and ":"")+(sec==0&&(i>0||toString(10)!=0)?"":B[sec])+(sec==0?"":" "+(typeof A[30+i]=="undefined"?"undefined".fontcolor("red"):A[30+i]))+(i==0||sec==0||(sec>0&&text=="")?"":",".fontcolor("white")+" ")+text;
}
}
if (num2En.arabNum.value.indexOf(".")!=-1) {
with (num2En.arabNum.value.split(".")[1].split(",").join("")) {
for (i=0;i<length;i++) {
sec = parseInt(substring(i,i+1),10);
text += (i==0?(text==""?A[0]:"")+" point ".fontcolor("white"):"")+(sec==0&&i!=0&&!parseInt(substring(i+1),10)>0?"":B[sec]+" ");
}
}
}
return readout.innerHTML = (text.indexOf("undefined")==-1?"":typeof alert("数字超长,超出部分将无法正确输出!")=="undefined"&&num2En.arabNum.focus()?"":"")+"结果输出:<br>"+text;
}
</script>
tigerwen01 2003-04-16
  • 打赏
  • 举报
回复
我想你提供一个VB程序示例:
Private Function NumToText(dblVal As Double) As String
Static Ones(0 To 9) As String
Static Teens(0 To 9) As String
Static Tens(0 To 9) As String
Static Thousands(0 To 4) As String
Static bInit As Boolean
Dim i As Integer, bAllZeros As Boolean, bShowThousands As Boolean
Dim strVal As String, strBuff As String, strTemp As String
Dim nCol As Integer, nChar As Integer

'Only handles positive values
Debug.Assert dblVal >= 0

If bInit = False Then
'Initialize array
bInit = True
Ones(0) = "zero"
Ones(1) = "one"
Ones(2) = "two"
Ones(3) = "three"
Ones(4) = "four"
Ones(5) = "five"
Ones(6) = "six"
Ones(7) = "seven"
Ones(8) = "eight"
Ones(9) = "nine"
Teens(0) = "ten"
Teens(1) = "eleven"
Teens(2) = "twelve"
Teens(3) = "thirteen"
Teens(4) = "fourteen"
Teens(5) = "fifteen"
Teens(6) = "sixteen"
Teens(7) = "seventeen"
Teens(8) = "eighteen"
Teens(9) = "nineteen"
Tens(0) = ""
Tens(1) = "ten"
Tens(2) = "twenty"
Tens(3) = "thirty"
Tens(4) = "forty"
Tens(5) = "fifty"
Tens(6) = "sixty"
Tens(7) = "seventy"
Tens(8) = "eighty"
Tens(9) = "ninety"
Thousands(0) = ""
Thousands(1) = "thousand" 'US numbering
Thousands(2) = "million"
Thousands(3) = "billion"
Thousands(4) = "trillion"
End If
'Trap errors
On Error GoTo NumToTextError
'Get fractional part
strBuff = "and " & Format((dblVal - Int(dblVal)) * 100, "00") & "/100"
'Convert rest to string and process each digit
strVal = CStr(Int(dblVal))
'Non-zero digit not yet encountered
bAllZeros = True
'Iterate through string
For i = Len(strVal) To 1 Step -1
'Get value of this digit
nChar = Val(Mid$(strVal, i, 1))
'Get column position
nCol = (Len(strVal) - i) + 1
'Action depends on 1's, 10's or 100's column
Select Case (nCol Mod 3)
Case 1 '1's position
bShowThousands = True
If i = 1 Then
'First digit in number (last in loop)
strTemp = Ones(nChar) & " "
ElseIf Mid$(strVal, i - 1, 1) = "1" Then
'This digit is part of "teen" number
strTemp = Teens(nChar) & " "
i = i - 1 'Skip tens position
ElseIf nChar > 0 Then
'Any non-zero digit
strTemp = Ones(nChar) & " "
Else
'This digit is zero. If digit in tens and hundreds column
'are also zero, don't show "thousands"
bShowThousands = False
'Test for non-zero digit in this grouping
If Mid$(strVal, i - 1, 1) <> "0" Then
bShowThousands = True
ElseIf i > 2 Then
If Mid$(strVal, i - 2, 1) <> "0" Then
bShowThousands = True
End If
End If
strTemp = ""
End If
'Show "thousands" if non-zero in grouping
If bShowThousands Then
If nCol > 1 Then
strTemp = strTemp & Thousands(nCol \ 3)
If bAllZeros Then
strTemp = strTemp & " "
Else
strTemp = strTemp & ", "
End If
End If
'Indicate non-zero digit encountered
bAllZeros = False
End If
strBuff = strTemp & strBuff
Case 2 '10's position
If nChar > 0 Then
If Mid$(strVal, i + 1, 1) <> "0" Then
strBuff = Tens(nChar) & "-" & strBuff
Else
strBuff = Tens(nChar) & " " & strBuff
End If
End If
Case 0 '100's position
If nChar > 0 Then
strBuff = Ones(nChar) & " hundred " & strBuff
End If
End Select
Next i
'Convert first letter to upper case
strBuff = UCase$(Left$(strBuff, 1)) & Mid$(strBuff, 2)
EndNumToText:
'Return result
NumToText = strBuff
Exit Function
NumToTextError:
strBuff = "#Error#"
Resume EndNumToText
End Function
colee 2003-04-15
  • 打赏
  • 举报
回复

××× 忘了补充,用Javascript写的 ×××

分不够再加。。。。






28,409

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧