js数字金额转换为英文金额(数字的中英文转化) 修正版-CarlZeng

function englishmoney(num){var i;var hundreds;var tenth;var one;var thousand;var million;var billion;var numbers;var svar result;if((num==’’)||(num==null)) return(‘’);var str;str= ‘000000000000000000 …

**function englishmoney(num){
var i;
var hundreds;
var tenth;
var one;
var thousand;
var million;
var billion;
var numbers;
var s
var result;

if((num==’’)||(num==null)) return(‘’);
var str;
str= ‘000000000000000000 ‘+num.toString();
numbers= ‘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 ‘;
String.prototype.Trim = function(){return this.replace(/^\s+|\s+$/g, “ “);}
String.prototype.Ltrim = function(){return this.replace(/^\s+/g, “ “);}
String.prototype.Rtrim = function(){return this.replace(/\s+$/g, “ “);}
s=str.substring(str.length-15,str.length);
billion=parseInt(s.substring(0,3)); //将12位整数分成4段:十亿、百万、千、百十个
million=parseInt(s.substring(3,6));
thousand=parseInt(s.substring(6,9));
result= ‘ ‘;
i=0;****
while(i <=3){
hundreds=parseInt(s.substring(i*3,i*3+1));//百位0-9
tenth=parseInt(s.substring(i*3+1,i*3+2));
if(tenth==1){one=10+parseInt(s.substring(i*3+2,i*3+3));}
else {one=0+parseInt(s.substring(i*3+2,i*3+3));}
// one=(CASE @tenth WHEN 1 THEN 10 ELSE 0 END)+CAST(SUBSTRING(@s,@i*3+3,1) AS int)–个位0-19
if(tenth <=1){tenth=0;}**

// tenth=(CASE WHEN @tenth <=1 THEN 0 ELSE @tenth END)–十位0、2-9
if((i==1&&parseInt(billion)> 0&&(parseInt(million)> 0||parseInt(thousand)> 0||parseInt(hundreds)> 0))||
(i==2&&(parseInt(billion)> 0||parseInt(million)> 0)&&(parseInt(thousand)> 0||parseInt(hundreds)> 0))||
(i==3&&(parseInt(billion)> 0||parseInt(million)> 0||parseInt(thousand)> 0)&&(parseInt(hundreds)> 0)))
{result=result+ ‘, ‘;}//–百位不是0则每段之间加连接符,

if(i==3 && (billion> 0 || million> 0 || thousand> 0) && (hundreds==0 && (tenth> 0 || one> 0)))
{result=result+ ‘ and ‘;}//–百位是0则加连接符AND

if(hundreds> 0)
{result=result+numbers.substring(hundreds*10-10,hundreds*10).Rtrim()+ ‘ hundred ‘;}

if( tenth>=2 && tenth <=9)
{
if(hundreds> 0)
{result=result+ ‘ and ‘;}
result=result+numbers.substring(tenth*10+170,tenth*10+180).Rtrim();
}

if(one>=1 && one <=19)
{
if(tenth> 0)
{result=result+ ‘- ‘;}
else
{
if(hundreds> 0){result=result+ ‘ and ‘;}
}
result=result+numbers.substring(one*10-10,one*10).Rtrim();
}

if(i==0 && parseInt(billion)> 0){
result=result+ ‘ billion ‘;}
if(i==1 && parseInt(million)> 0){
result=result+ ‘ million ‘;}
if(i==2 && parseInt(thousand)> 0){
result=result+ ‘ thousand ‘;}
i=i+1;
}
if(s.substring(13,15)!= ‘00 ‘){
result=result+ ‘ Only ‘;
if(s.substring(13,14)== ‘0 ‘){
result=result+ ‘zero ‘;}
else
{result=result+numbers.substring(parseInt(s.substring(13,14))*10-10,parseInt(s.substring(13,14))*10).Rtrim();}
if(s.substring(14,15)!= ‘0 ‘){
result=result+ ‘ ‘+numbers.substring(parseInt(s.substring(14,15))*10-10,parseInt(s.substring(14,15))*10).Rtrim();}
}

return(result);
}

以上js实现 阿拉伯数字金额 转换为英文金额

请注意修改信息:str= ‘000000000000000000 ‘+num.toString();//最后一个0后面有一个空格总共18个0+1个空格19个字符。over

使用方法:englishmoney( subtotal )

**englishmoney( 86833000.00) 结果会是:eighty - six eight hundred and thirty - three thousand Only
englishmoney( 12334.00) 结果会是:twelve thousand , three hundred and thirty - four Only
**