zoukankan      html  css  js  c++  java
  • js阿拉伯数字转中文大写

    第一种方法:

    function DX(n) { //金额大写转换函数
    if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
    return "数据非法";
    var unit = "千百拾亿千百拾万千百拾元角分", str = "";
    n += "00";
    var p = n.indexOf('.');
    if (p >= 0)
    n = n.substring(0, p) + n.substr(p+1, 2);
    unit = unit.substr(unit.length - n.length);
    for (var i=0; i < n.length; i++)
    str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
    return str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "元整");
    }

    第二种方法:

    function i2c(n){
    if(typeof(n) == "number"){
    if(-1 == n.toString().indexOf(".")){
    return seti2c(n);
    }else{
    var i,istr,f,fstr,a,rstr;
    a = n.toString().split(".");
    i = a[0];
    f = a[1];
    istr = seti2c(i);
    fstr = setf2c(f);
    rstr = istr+"零"+fstr;
    rstr = rstr.replace(/零+/g,"零");
    return rstr;
    }
    }else{
    return "---";
    }
    }

    function seti2c(n){
    var ns = n.toString();
    var tempstr = "";
    for(var i=1;i<ns.length+1;i++){
    switch(i){
    case 1:
    var t = ns.substr(ns.length-i,1);
    if(t!="0"){
    tempstr = t+"圆"+tempstr;
    }else{
    tempstr = "圆"+tempstr;
    }
    break;
    case 2:
    var t = ns.substr(ns.length-i,1);
    if(t!="0"){
    tempstr = t+"拾"+tempstr;
    }else{
    tempstr = "0"+tempstr;
    }
    break;
    case 3:
    var t = ns.substr(ns.length-i,1);
    if(t!="0"){
    tempstr = t+"佰"+tempstr;
    }else{
    tempstr = "0"+tempstr;
    }
    break;
    case 4:
    var t = ns.substr(ns.length-i,1);
    if(t!="0"){
    tempstr = t+"仟"+tempstr;
    }else{
    tempstr = "0"+tempstr;
    }
    break;
    case 5:
    var t = ns.substr(ns.length-i,1);
    if(t!="0"){
    tempstr = t+"万"+tempstr;
    }else{
    tempstr = "万"+tempstr;
    }
    break;
    case 6:
    var t = ns.substr(ns.length-i,1);
    if(t!="0"){
    tempstr = t+"拾"+tempstr;
    }else{
    tempstr = "0"+tempstr;
    }
    break;
    case 7:
    var t = ns.substr(ns.length-i,1);
    if(t!="0"){
    tempstr = t+"佰"+tempstr;
    }else{
    tempstr = "0"+tempstr;
    }
    break;
    case 8:
    var t = ns.substr(ns.length-i,1);
    if(t!="0"){
    tempstr = t+"仟"+tempstr;
    }else{
    tempstr = "0"+tempstr;
    }
    break;
    case 9:
    var t = ns.substr(ns.length-i,1);
    if(t!="0"){
    tempstr = t+"亿"+tempstr;
    }else{
    tempstr = "亿"+tempstr;
    }
    break;
    case 10:
    var t = ns.substr(ns.length-i,1);
    if(t!="0"){
    tempstr = t+"拾"+tempstr;
    }else{
    tempstr = "0"+tempstr;
    }
    break;
    case 11:
    var t = ns.substr(ns.length-i,1);
    if(t!="0"){
    tempstr = t+"佰"+tempstr;
    }else{
    tempstr = "0"+tempstr;
    }
    break;
    case 12:
    var t = ns.substr(ns.length-i,1);
    if(t!="0"){
    tempstr = t+"仟"+tempstr;
    }else{
    tempstr = "0"+tempstr;
    }
    break;
    default:break;
    }
    }
    return g2b(tempstr);
    }

    function setf2c(n){
    var ns = n.toString();
    var tempstr = "";
    for(var i=0;i<ns.length;i++){
    switch(i){
    case 0:
    var t = ns.substr(i,1);
    if(t!="0"){
    tempstr += t+"角";
    }else{
    tempstr += "0";
    }
    break;
    case 1:
    var t = ns.substr(i,1);
    if(t!="0"){
    tempstr += t+"分";
    }
    break;
    default:break;
    }
    }
    return g2b(tempstr);
    }

    function g2b(s){
    rs = s.replace(/0+/g,"0");
    rs = rs.replace("0圆","圆");
    rs = rs.replace("0亿","亿");
    rs = rs.replace("0万","万");
    rs = rs.replace("亿万","亿");
    rs = rs.replace(/0/g,"零");
    rs = rs.replace(/1/g,"壹");
    rs = rs.replace(/2/g,"贰");
    rs = rs.replace(/3/g,"叁");
    rs = rs.replace(/4/g,"肆");
    rs = rs.replace(/5/g,"伍");
    rs = rs.replace(/6/g,"陆");
    rs = rs.replace(/7/g,"柒");
    rs = rs.replace(/8/g,"捌");
    rs = rs.replace(/9/g,"玖");
    return rs;
    }

  • 相关阅读:
    block本质探寻五之atuto类型局部实例对象
    block本质探寻四之copy
    block本质探寻三之block类型
    Android Studio使用时遇见的一些问题+解决方法(持续更新)
    Android之练习MVVM+DataBinding框架模式
    解决 Mac Android Studio Gradle Sync 慢的问题
    Android之MVC、MVP、MVVM
    Android之同步与异步
    Android之Fragment
    Android之Fragment栈操作 commit()问题分析
  • 原文地址:https://www.cnblogs.com/zyxzhsh/p/1854476.html
Copyright © 2011-2022 走看看