zoukankan      html  css  js  c++  java
  • js数字金额转换为英文金额(数字的中英文转化) 修正版

    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

  • 相关阅读:
    HDU 4389 数位dp
    Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)A B C 水 并查集 思路
    Codeforces Round #385 (Div. 2)A B C 模拟 水 并查集
    Codeforces Round #404 (Div. 2)A B C二分
    HDU 2586 倍增法求lca
    Codeforces Round #209 (Div. 2)A贪心 B思路 C思路+快速幂
    Codeforces Round #384 (Div. 2) A B C D dfs序+求两个不相交区间 最大权值和
    vim出现“E212: Can't open file for writing”的处理办法
    centos7 开机/etc/rc.local 不执行的问题
    CentOS 系统状况查看
  • 原文地址:https://www.cnblogs.com/backuper/p/1339852.html
Copyright © 2011-2022 走看看