程序代码如下,请高手帮忙:
'---------------- N U M B E R S --------------------- N U M B E R S ----------------
Sub playZero(batch)
call addOrd(batch, (pathToNumbers & "0.wav") )
End Sub
Sub play2LastDigits(numValue, batch)
Select Case numValue
Case 0
'say nothing
Case 1
call addOrd(batch, (pathToNumbers & "1.wav") )
Case 2
call addOrd(batch, (pathToNumbers & "2.wav") )
Case 3
call addOrd(batch, (pathToNumbers & "3.wav") )
Case 4
call addOrd(batch, (pathToNumbers & "4.wav") )
Case 5
call addOrd(batch, (pathToNumbers & "5.wav") )
Case 6
call addOrd(batch, (pathToNumbers & "6.wav") )
Case 7
call addOrd(batch, (pathToNumbers & "7.wav") )
Case 8
call addOrd(batch, (pathToNumbers & "8.wav") )
Case 9
call addOrd(batch, (pathToNumbers & "9.wav") )
Case 10
call addOrd(batch, (pathToNumbers & "10.wav") )
'---------------------------------------------------------------------- 10
Case 11
call addOrd(batch, (pathToNumbers & "11.wav") )
'------------------中间的部分省略了
Case 95
call addOrd(batch, (pathToNumbers & "95.wav") )
Case 96
call addOrd(batch, (pathToNumbers & "96.wav") )
Case 97
call addOrd(batch, (pathToNumbers & "97.wav") )
Case 98
call addOrd(batch, (pathToNumbers & "98.wav") )
Case 99
call addOrd(batch, (pathToNumbers & "99.wav") )
'---------------------------------------------------------------------- 99
Case Else
playPhrase("str=" & CStr(numValue))
End Select
End Sub
Sub playHundreds(hundreds, batch)
Select Case hundreds
Case 0
'say nothing
Case 100
call addOrd(batch, (pathToNumbers & "100.wav") )
Case 200
call addOrd(batch, (pathToNumbers & "200.wav") )
Case 300
call addOrd(batch, (pathToNumbers & "300.wav") )
Case 400
call addOrd(batch, (pathToNumbers & "400.wav") )
Case 500
call addOrd(batch, (pathToNumbers & "500.wav") )
Case 600
call addOrd(batch, (pathToNumbers & "600.wav") )
Case 700
call addOrd(batch, (pathToNumbers & "700.wav") )
Case 800
call addOrd(batch, (pathToNumbers & "800.wav") )
Case 900
call addOrd(batch, (pathToNumbers & "900.wav") )
End Select
End Sub
Sub play3Digits(numValue, batch)
Dim hundreds, last2digits, qians
qians = (numValue - (numValue Mod 1000))/1000
If qians<>0 Then
playSomeThousands qians, batch
playAnd batch
End If
numValue = numValue - qians*1000
last2digits = numValue Mod 100
hundreds = numValue - last2digits
If hundreds<>0 Then
playHundreds hundreds, batch
playAnd batch
End If
play2LastDigits last2digits, batch
End Sub
'--------------- NB! There is NO "and.wav" file in the voxeo library
Sub playAnd(batch)
REM ------------------------ the word "AND" is disabled because it is
REM ------------------------ spoken by male voice -------------------
REM call addOrd(batch, (pathToMiscWords & "and_2.wav") )
End Sub
Sub playMinus(batch)
call addOrd(batch, (pathToMath & "minus.wav") )
End Sub
Sub playBillion(batch)
call addOrd(batch, (pathToNumbers & "billion.wav") )
End Sub
Sub playMillion(batch)
call addOrd(batch, (pathToNumbers & "yi.wav") )
End Sub
Sub playThousand(batch)
call addOrd(batch, (pathToNumbers & "wan.wav") )
End Sub
Function playSomeThousands(cur3Digits, batch)
playSomeThousands = True
Select Case cur3Digits
Case 1
call addOrd(batch, (pathToNumbers & "1000.wav") )
Case 2
call addOrd(batch, (pathToNumbers & "2000.wav") )
Case 3
call addOrd(batch, (pathToNumbers & "3000.wav") )
Case 4
call addOrd(batch, (pathToNumbers & "4000.wav") )
Case 5
call addOrd(batch, (pathToNumbers & "5000.wav") )
Case 6
call addOrd(batch, (pathToNumbers & "6000.wav") )
Case 7
call addOrd(batch, (pathToNumbers & "7000.wav") )
Case 8
call addOrd(batch, (pathToNumbers & "8000.wav") )
Case 9
call addOrd(batch, (pathToNumbers & "9000.wav") )
Case 10
call addOrd(batch, (pathToNumbers & "10000.wav") )
Case 0
call addOrd(batch, (pathToNumbers & "9000.wav") )
Case Else
playSomeThousands = False
End Select
End Function
Function playSomeThousands_new(cur3Digits, batch)
'playSomeThousands = True
Select Case cur3Digits
Case 1
call addOrd(batch, (pathToNumbers & "1000.wav") )
Case 2
call addOrd(batch, (pathToNumbers & "2000.wav") )
Case 3
call addOrd(batch, (pathToNumbers & "3000.wav") )
Case 4
call addOrd(batch, (pathToNumbers & "4000.wav") )
Case 5
call addOrd(batch, (pathToNumbers & "5000.wav") )
Case 6
call addOrd(batch, (pathToNumbers & "6000.wav") )
Case 7
call addOrd(batch, (pathToNumbers & "7000.wav") )
Case 8
call addOrd(batch, (pathToNumbers & "8000.wav") )
Case 9
call addOrd(batch, (pathToNumbers & "9000.wav") )
Case 0
'say nothing
Case Else
'playSomeThousands = False
End Select
End Function
Sub playNum(strValue, batch)
Dim numValue
If Not IsNumeric(strValue) Then
strErr = "Error: argument of playNum is not a number: " & strValue
Exit Sub
End If
Err.Clear
On Error Resume Next
numValue = CLng(strValue) ' this may produce an error if the number
' is greater than 2,147,483,647
' therefore On Error Resume Next is used
If Err Then
strErr = "Error: argument for playNum() is too large: " & strValue & vbCrLf & Err.description
Err.Clear
Exit Sub
End If
If numValue=0 Then
playZero batch
Exit Sub
Else
Dim nDigits, nNumber, cur3Digits, curFactor
Dim i
If numValue<0 Then
numValue = numValue * (-1)
playMinus batch
End If
nDigits = Len(CStr(numValue))
'MsgBox "1. nDigits = " & nDigits ' for debugging
nNumber = (nDigits - (nDigits Mod 4))/4
If (nDigits Mod 4) = 0 And nNumber>0 Then
nNumber = nNumber - 1
End If
'
'英文习惯 Relationshitp of nNumber and nDigits
'-----------------------------------------
' nNumber nDigits
' ----------------------------------------
' 0 1,2,3
' 1 4,5,6 (thousands)
' 2 7,8,9 (millions)
' 3 10 (1 or 2 billions)
'-----------------------------------------
'
'
'中文习惯 Relationshitp of nNumber and nDigits
'-----------------------------------------
' nNumber nDigits
' ----------------------------------------
' 0 1,2,3,4
' 1 5,6,7,8 (thousands)
' 2 9,10,11,12 (millions)
' 3 13 (1 or 2 billions)
'-----------------------------------------
'
'MsgBox "2. nNumber = " & nNumber ' for debugging
For i=nNumber to 0 Step -1
Select Case i
Case 3
curFactor = 1000000000000
cur3Digits = (numValue - (numValue Mod curFactor))/curFactor
If cur3Digits>0 Then
play3Digits cur3Digits, batch
playBillion batch
End If
numValue = numValue - cur3Digits*curFactor
Case 2
curFactor = 100000000
cur3Digits = (numValue - (numValue Mod curFactor))/curFactor
If cur3Digits>0 Then
play3Digits cur3Digits, batch
playMillion batch
End If
numValue = numValue - cur3Digits*curFactor
Case 1 'thousands
curFactor = 10000
cur3Digits = (numValue - (numValue Mod curFactor))/curFactor
If cur3Digits>0 Then
'If Not playSomeThousands(cur3Digits, batch) Then
play3Digits cur3Digits, batch
playThousand batch
'End If
End If
numValue = numValue - cur3Digits*curFactor
Case 0
play3Digits numValue, batch
End Select
Next
End If
End Sub
'------- E N D O F N U M B E R S ---------- E N D O F N U M B E R S ---------
9 个解决方案
#1
你这个哪是JS程序,发错地方了
不过等待高手来回!
不过等待高手来回!
#2
楼主的是vbscript程序
不过代码太长,看不下去
不过代码太长,看不下去
#3
好恶心的 switch case,呵呵
英文中金额的阅读方式与中文不同,原来的代码似乎是针对英文设计的,
如果需要中文方式的金额阅读,就需要认真分析,而不是匆忙滴修改代码!
英文中金额的阅读方式与中文不同,原来的代码似乎是针对英文设计的,
如果需要中文方式的金额阅读,就需要认真分析,而不是匆忙滴修改代码!
#4
没办法,只能对原来英文播报方式进行修改。请各位脚本高手帮忙!
#5
问问楼主 这样的 switch case
怎么改的好看些
我也觉得恶心
但是每次写都只能这么写 找不出什么好方式
#6
俺写一个试试看,会给出输出大写中文的版本,至于音频版 lz 自己把字改为音频文件即可!
稍等哈
#7
好的,多谢。我参考参考
#8
<script>
function chineseNumber(num)
{
if (isNaN(num) || num > Math.pow(10, 12))
return "";
var cn = "零壹贰叁肆伍陆柒捌玖"
var unit = new Array("拾佰仟", "分角")
var unit1= new Array("万亿", "")
var numArray = num.toString().split(".")
var start = new Array(numArray[0].length-1, 2)
function toChinese(num, index)
{
var num = num.replace(/\d/g, function ($1)
{
return cn.charAt($1)+unit[index].charAt(start--%4 ? start%4 : -1)
})
return num
}
for (var i=0; i<numArray.length; i++)
{
var tmp = ""
for (var j=0; j*4<numArray[i].length; j++)
{
var strIndex = numArray[i].length-(j+1)*4
var str = numArray[i].substring(strIndex, strIndex+4)
var start = i ? 2 : str.length-1
var tmp1 = toChinese(str, i)
tmp1 = tmp1.replace(/(零.)+/g, "零").replace(/零+$/, "")
tmp1 = tmp1.replace(/^壹拾/, "拾")
tmp = (tmp1+unit1[i].charAt(j-1)) + tmp
}
numArray[i] = tmp
}
numArray[1] = numArray[1] ? numArray[1] : ""
numArray[0] = numArray[0] ? numArray[0]+"圆" : numArray[0], numArray[1] = numArray[1].replace(/^零+/, "")
numArray[1] = numArray[1].match(/分/) ? numArray[1] : numArray[1]+"整"
return numArray[0]+numArray[1]
}
function aNumber(num)
{
var numArray = new Array()
var unit = "亿万圆$"
for (var i=0; i<unit.length; i++)
{
var re = eval("/"+ (numArray[i-1] ? unit.charAt(i-1) : "") +"(.*)"+unit.charAt(i)+"/")
if (num.match(re))
{
numArray[i] = num.match(re)[1].replace(/^拾/, "壹拾")
numArray[i] = numArray[i].replace(/[零壹贰叁肆伍陆柒捌玖]/g, function ($1)
{
return "零壹贰叁肆伍陆柒捌玖".indexOf($1)
})
numArray[i] = numArray[i].replace(/[分角拾佰仟]/g, function ($1)
{
return "*"+Math.pow(10, "分角 拾佰仟 ".indexOf($1)-2)+"+"
}).replace(/^\*|\+$/g, "").replace(/整/, "0")
numArray[i] = "(" + numArray[i] + ")*"+Math.ceil(Math.pow(10, (2-i)*4))
}
else numArray[i] = 0
}
return eval(numArray.join("+"))
}
</script>
<!--onkeypress="regInput(event,/^\d{0,12} (\. \d{0,2})?$/)"-->
<input id=up size=60 ><button onclick="lw.value = chineseNumber(up.value)">转为大写</button><br>
<input id=lw size=60 value="壹仟壹佰壹拾壹圆整"><button onclick="up.value = aNumber(lw.value)">转为小写</button>
#9
不好意思,昨天临时有事没写完,今天补上,几乎完成了,
lz 须要自己重新录音,代码简单修改就可以用了
L@_@K
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ParseChineseFigureAmountStringIntoChineseWords</title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
<body>
<script type="text/javascript">
<!--
String.Empty = "";
String.prototype.Trim = function() {
return this.replace(/(^\s+)|(\s+$)/g, String.Empty);
};
String.prototype.TrimChar = function(trimedChar) {
var reg = new RegExp("(^" + trimedChar + "+)|(" + trimedChar + "+$)", "g");
return this.replace(reg, String.Empty);
};
String.prototype.RightTrimChar = function(trimedChar) {
var reg = new RegExp(trimedChar + "+$", "g");
return this.replace(reg, String.Empty);
};
String.prototype.LeftTrimChar = function(trimedChar) {
var reg = new RegExp("^" + trimedChar + "+", "g");
return this.replace(reg, String.Empty);
};
String.prototype.IsEndWith = function(endString) {
var reg = new RegExp(endString + "+$", "g");
return reg.test(this);
};
function ParseAmountStringIntoChineseWords(amountString)
{
var figureString = GetFigureStringFromAmountString(amountString);
if (figureString == null)
throw new Error("输入金额文本格式非法!");
// head.tail
var figureParts = figureString.split(".");
var headPart = figureParts[0];
var tailPart = null;
if (figureParts.length > 1)
tailPart = figureParts[1];
var headString = GetHeadStringByHeadPart(headPart);
var tailString = GetTailStringByTailPart(tailPart);
return JoinHeadStringAndTailString(headString, tailString);
}
function JoinHeadStringAndTailString(headString, tailString)
{
var result = String.Empty;
if (headString != String.Empty
&& tailString == String.Empty)
result = headString + "整";
else if (headString == String.Empty
&& tailString != String.Empty)
result = tailString.LeftTrimChar('零');
else
result = headString + tailString;
return result;
}
function GetFigureStringFromAmountString(amountString)
{
amountString = amountString.Trim();
var amountReg = /^¥(\d+(\.(\d+))?)/;
if (amountReg.test(amountString))
return amountString.match(amountReg)[1].Trim();
else
return null;
}
function GetHeadStringByHeadPart(headPart)
{
var headFullUnits = ["元","拾","百","千","万","拾","百","千","亿","拾","百","千","万"];
var yuanUnit = headFullUnits[0];
var headString = ParseFigureStringIntoChineseWords(headPart, headFullUnits);
headString = headString.TrimChar('零');
if (headString != String.Empty
&& !headString.IsEndWith(yuanUnit))
headString += yuanUnit;
return headString;
}
function GetTailStringByTailPart(tailPart)
{
var tailFullUnits = ["分","角"];
var tailString = ParseFigureStringIntoChineseWords(tailPart, tailFullUnits);
tailString = tailString.RightTrimChar('零');
return tailString;
}
function ParseFigureStringIntoChineseWords(figureString, unitArray)
{
var chineseWords = String.Empty;
if (figureString != null && figureString != String.Empty)
{
var usedUnits = GetMatchingUnitsOfFigures(unitArray, figureString);
var chineseNumbers = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"];
var figures = figureString.split(String.Empty);
for (var i=0; i<figures.length; i++)
{
chineseWords += chineseNumbers[parseInt(figures[i])];
if (figures[i] != "0")
chineseWords += usedUnits[i];
}
chineseWords = ReplaceMultipleChineseZeroWordsWithSingle(chineseWords);
}
return chineseWords;
}
function GetMatchingUnitsOfFigures(unitArray, figureString)
{
return unitArray.slice(0, figureString.length).reverse();
}
function ReplaceMultipleChineseZeroWordsWithSingle(chineseWords)
{
return chineseWords.replace(/零{2,}/g, "零");
}
// -----------------------------------------------
// Test
// -----------------------------------------------
function TestAndShowOutput(amountString)
{
var Sounder = {};
Sounder["零"] = "0.wav";
Sounder["壹"] = "1.wav";
Sounder["贰"] = "2.wav";
Sounder["叁"] = "3.wav";
Sounder["肆"] = "4.wav";
Sounder["伍"] = "5.wav";
Sounder["陆"] = "6.wav";
Sounder["柒"] = "7.wav";
Sounder["捌"] = "8.wav";
Sounder["玖"] = "9.wav";
Sounder["分"] = "fen.wav";
Sounder["角"] = "jiao.wav";
Sounder["元"] = "yuan.wav";
Sounder["整"] = "zheng.wav";
Sounder["拾"] = "shi.wav";
Sounder["百"] = "bai.wav";
Sounder["千"] = "qian.wav";
Sounder["万"] = "wan.wav";
Sounder["亿"] = "yi.wav";
document.write(amountString, ": ");
var chineseWords = ParseAmountStringIntoChineseWords(amountString);
document.write(chineseWords);
if (chineseWords != String.Empty)
{
var speeches = chineseWords.split(String.Empty);
for (var i=0; i<speeches.length; i++)
speeches[i] = Sounder[speeches[i]];
document.write("; Pronounce: ", speeches.join("-"));
}
document.write("<br />");
}
TestAndShowOutput("¥10203");
TestAndShowOutput("¥10200");
TestAndShowOutput("¥10003");
TestAndShowOutput("¥10000");
TestAndShowOutput("¥0.40");
TestAndShowOutput("¥0.45");
TestAndShowOutput("¥0.05");
TestAndShowOutput("¥10203.40");
TestAndShowOutput("¥10200.40");
TestAndShowOutput("¥10203.00");
TestAndShowOutput("¥0.00");
/*
¥10203: 壹万零贰百零叁元整; Pronounce: 1.wav-wan.wav-0.wav-2.wav-bai.wav-0.wav-3.wav-yuan.wav-zheng.wav
¥10200: 壹万零贰百元整; Pronounce: 1.wav-wan.wav-0.wav-2.wav-bai.wav-yuan.wav-zheng.wav
¥10003: 壹万零叁元整; Pronounce: 1.wav-wan.wav-0.wav-3.wav-yuan.wav-zheng.wav
¥10000: 壹万元整; Pronounce: 1.wav-wan.wav-yuan.wav-zheng.wav
¥0.40: 肆角; Pronounce: 4.wav-jiao.wav
¥0.45: 肆角伍分; Pronounce: 4.wav-jiao.wav-5.wav-fen.wav
¥0.05: 伍分; Pronounce: 5.wav-fen.wav
¥10203.40: 壹万零贰百零叁元肆角; Pronounce: 1.wav-wan.wav-0.wav-2.wav-bai.wav-0.wav-3.wav-yuan.wav-4.wav-jiao.wav
¥10200.40: 壹万零贰百元肆角; Pronounce: 1.wav-wan.wav-0.wav-2.wav-bai.wav-yuan.wav-4.wav-jiao.wav
¥10203.00: 壹万零贰百零叁元整; Pronounce: 1.wav-wan.wav-0.wav-2.wav-bai.wav-0.wav-3.wav-yuan.wav-zheng.wav
¥0.00:
*/
//-->
</script>
</body>
</html>
#1
你这个哪是JS程序,发错地方了
不过等待高手来回!
不过等待高手来回!
#2
楼主的是vbscript程序
不过代码太长,看不下去
不过代码太长,看不下去
#3
好恶心的 switch case,呵呵
英文中金额的阅读方式与中文不同,原来的代码似乎是针对英文设计的,
如果需要中文方式的金额阅读,就需要认真分析,而不是匆忙滴修改代码!
英文中金额的阅读方式与中文不同,原来的代码似乎是针对英文设计的,
如果需要中文方式的金额阅读,就需要认真分析,而不是匆忙滴修改代码!
#4
没办法,只能对原来英文播报方式进行修改。请各位脚本高手帮忙!
#5
问问楼主 这样的 switch case
怎么改的好看些
我也觉得恶心
但是每次写都只能这么写 找不出什么好方式
#6
俺写一个试试看,会给出输出大写中文的版本,至于音频版 lz 自己把字改为音频文件即可!
稍等哈
#7
好的,多谢。我参考参考
#8
<script>
function chineseNumber(num)
{
if (isNaN(num) || num > Math.pow(10, 12))
return "";
var cn = "零壹贰叁肆伍陆柒捌玖"
var unit = new Array("拾佰仟", "分角")
var unit1= new Array("万亿", "")
var numArray = num.toString().split(".")
var start = new Array(numArray[0].length-1, 2)
function toChinese(num, index)
{
var num = num.replace(/\d/g, function ($1)
{
return cn.charAt($1)+unit[index].charAt(start--%4 ? start%4 : -1)
})
return num
}
for (var i=0; i<numArray.length; i++)
{
var tmp = ""
for (var j=0; j*4<numArray[i].length; j++)
{
var strIndex = numArray[i].length-(j+1)*4
var str = numArray[i].substring(strIndex, strIndex+4)
var start = i ? 2 : str.length-1
var tmp1 = toChinese(str, i)
tmp1 = tmp1.replace(/(零.)+/g, "零").replace(/零+$/, "")
tmp1 = tmp1.replace(/^壹拾/, "拾")
tmp = (tmp1+unit1[i].charAt(j-1)) + tmp
}
numArray[i] = tmp
}
numArray[1] = numArray[1] ? numArray[1] : ""
numArray[0] = numArray[0] ? numArray[0]+"圆" : numArray[0], numArray[1] = numArray[1].replace(/^零+/, "")
numArray[1] = numArray[1].match(/分/) ? numArray[1] : numArray[1]+"整"
return numArray[0]+numArray[1]
}
function aNumber(num)
{
var numArray = new Array()
var unit = "亿万圆$"
for (var i=0; i<unit.length; i++)
{
var re = eval("/"+ (numArray[i-1] ? unit.charAt(i-1) : "") +"(.*)"+unit.charAt(i)+"/")
if (num.match(re))
{
numArray[i] = num.match(re)[1].replace(/^拾/, "壹拾")
numArray[i] = numArray[i].replace(/[零壹贰叁肆伍陆柒捌玖]/g, function ($1)
{
return "零壹贰叁肆伍陆柒捌玖".indexOf($1)
})
numArray[i] = numArray[i].replace(/[分角拾佰仟]/g, function ($1)
{
return "*"+Math.pow(10, "分角 拾佰仟 ".indexOf($1)-2)+"+"
}).replace(/^\*|\+$/g, "").replace(/整/, "0")
numArray[i] = "(" + numArray[i] + ")*"+Math.ceil(Math.pow(10, (2-i)*4))
}
else numArray[i] = 0
}
return eval(numArray.join("+"))
}
</script>
<!--onkeypress="regInput(event,/^\d{0,12} (\. \d{0,2})?$/)"-->
<input id=up size=60 ><button onclick="lw.value = chineseNumber(up.value)">转为大写</button><br>
<input id=lw size=60 value="壹仟壹佰壹拾壹圆整"><button onclick="up.value = aNumber(lw.value)">转为小写</button>
#9
不好意思,昨天临时有事没写完,今天补上,几乎完成了,
lz 须要自己重新录音,代码简单修改就可以用了
L@_@K
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ParseChineseFigureAmountStringIntoChineseWords</title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
<body>
<script type="text/javascript">
<!--
String.Empty = "";
String.prototype.Trim = function() {
return this.replace(/(^\s+)|(\s+$)/g, String.Empty);
};
String.prototype.TrimChar = function(trimedChar) {
var reg = new RegExp("(^" + trimedChar + "+)|(" + trimedChar + "+$)", "g");
return this.replace(reg, String.Empty);
};
String.prototype.RightTrimChar = function(trimedChar) {
var reg = new RegExp(trimedChar + "+$", "g");
return this.replace(reg, String.Empty);
};
String.prototype.LeftTrimChar = function(trimedChar) {
var reg = new RegExp("^" + trimedChar + "+", "g");
return this.replace(reg, String.Empty);
};
String.prototype.IsEndWith = function(endString) {
var reg = new RegExp(endString + "+$", "g");
return reg.test(this);
};
function ParseAmountStringIntoChineseWords(amountString)
{
var figureString = GetFigureStringFromAmountString(amountString);
if (figureString == null)
throw new Error("输入金额文本格式非法!");
// head.tail
var figureParts = figureString.split(".");
var headPart = figureParts[0];
var tailPart = null;
if (figureParts.length > 1)
tailPart = figureParts[1];
var headString = GetHeadStringByHeadPart(headPart);
var tailString = GetTailStringByTailPart(tailPart);
return JoinHeadStringAndTailString(headString, tailString);
}
function JoinHeadStringAndTailString(headString, tailString)
{
var result = String.Empty;
if (headString != String.Empty
&& tailString == String.Empty)
result = headString + "整";
else if (headString == String.Empty
&& tailString != String.Empty)
result = tailString.LeftTrimChar('零');
else
result = headString + tailString;
return result;
}
function GetFigureStringFromAmountString(amountString)
{
amountString = amountString.Trim();
var amountReg = /^¥(\d+(\.(\d+))?)/;
if (amountReg.test(amountString))
return amountString.match(amountReg)[1].Trim();
else
return null;
}
function GetHeadStringByHeadPart(headPart)
{
var headFullUnits = ["元","拾","百","千","万","拾","百","千","亿","拾","百","千","万"];
var yuanUnit = headFullUnits[0];
var headString = ParseFigureStringIntoChineseWords(headPart, headFullUnits);
headString = headString.TrimChar('零');
if (headString != String.Empty
&& !headString.IsEndWith(yuanUnit))
headString += yuanUnit;
return headString;
}
function GetTailStringByTailPart(tailPart)
{
var tailFullUnits = ["分","角"];
var tailString = ParseFigureStringIntoChineseWords(tailPart, tailFullUnits);
tailString = tailString.RightTrimChar('零');
return tailString;
}
function ParseFigureStringIntoChineseWords(figureString, unitArray)
{
var chineseWords = String.Empty;
if (figureString != null && figureString != String.Empty)
{
var usedUnits = GetMatchingUnitsOfFigures(unitArray, figureString);
var chineseNumbers = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"];
var figures = figureString.split(String.Empty);
for (var i=0; i<figures.length; i++)
{
chineseWords += chineseNumbers[parseInt(figures[i])];
if (figures[i] != "0")
chineseWords += usedUnits[i];
}
chineseWords = ReplaceMultipleChineseZeroWordsWithSingle(chineseWords);
}
return chineseWords;
}
function GetMatchingUnitsOfFigures(unitArray, figureString)
{
return unitArray.slice(0, figureString.length).reverse();
}
function ReplaceMultipleChineseZeroWordsWithSingle(chineseWords)
{
return chineseWords.replace(/零{2,}/g, "零");
}
// -----------------------------------------------
// Test
// -----------------------------------------------
function TestAndShowOutput(amountString)
{
var Sounder = {};
Sounder["零"] = "0.wav";
Sounder["壹"] = "1.wav";
Sounder["贰"] = "2.wav";
Sounder["叁"] = "3.wav";
Sounder["肆"] = "4.wav";
Sounder["伍"] = "5.wav";
Sounder["陆"] = "6.wav";
Sounder["柒"] = "7.wav";
Sounder["捌"] = "8.wav";
Sounder["玖"] = "9.wav";
Sounder["分"] = "fen.wav";
Sounder["角"] = "jiao.wav";
Sounder["元"] = "yuan.wav";
Sounder["整"] = "zheng.wav";
Sounder["拾"] = "shi.wav";
Sounder["百"] = "bai.wav";
Sounder["千"] = "qian.wav";
Sounder["万"] = "wan.wav";
Sounder["亿"] = "yi.wav";
document.write(amountString, ": ");
var chineseWords = ParseAmountStringIntoChineseWords(amountString);
document.write(chineseWords);
if (chineseWords != String.Empty)
{
var speeches = chineseWords.split(String.Empty);
for (var i=0; i<speeches.length; i++)
speeches[i] = Sounder[speeches[i]];
document.write("; Pronounce: ", speeches.join("-"));
}
document.write("<br />");
}
TestAndShowOutput("¥10203");
TestAndShowOutput("¥10200");
TestAndShowOutput("¥10003");
TestAndShowOutput("¥10000");
TestAndShowOutput("¥0.40");
TestAndShowOutput("¥0.45");
TestAndShowOutput("¥0.05");
TestAndShowOutput("¥10203.40");
TestAndShowOutput("¥10200.40");
TestAndShowOutput("¥10203.00");
TestAndShowOutput("¥0.00");
/*
¥10203: 壹万零贰百零叁元整; Pronounce: 1.wav-wan.wav-0.wav-2.wav-bai.wav-0.wav-3.wav-yuan.wav-zheng.wav
¥10200: 壹万零贰百元整; Pronounce: 1.wav-wan.wav-0.wav-2.wav-bai.wav-yuan.wav-zheng.wav
¥10003: 壹万零叁元整; Pronounce: 1.wav-wan.wav-0.wav-3.wav-yuan.wav-zheng.wav
¥10000: 壹万元整; Pronounce: 1.wav-wan.wav-yuan.wav-zheng.wav
¥0.40: 肆角; Pronounce: 4.wav-jiao.wav
¥0.45: 肆角伍分; Pronounce: 4.wav-jiao.wav-5.wav-fen.wav
¥0.05: 伍分; Pronounce: 5.wav-fen.wav
¥10203.40: 壹万零贰百零叁元肆角; Pronounce: 1.wav-wan.wav-0.wav-2.wav-bai.wav-0.wav-3.wav-yuan.wav-4.wav-jiao.wav
¥10200.40: 壹万零贰百元肆角; Pronounce: 1.wav-wan.wav-0.wav-2.wav-bai.wav-yuan.wav-4.wav-jiao.wav
¥10203.00: 壹万零贰百零叁元整; Pronounce: 1.wav-wan.wav-0.wav-2.wav-bai.wav-0.wav-3.wav-yuan.wav-zheng.wav
¥0.00:
*/
//-->
</script>
</body>
</html>