C#实现,C++实现,JS实现 阿拉伯数字金额转换为中文大写金额

推荐在线编译器  ideone

1、 C#实现 :带有负数处理(已验证可用 

 /// <summary>
 /// 把数字金额转换成中文大写数字的函数(带有负值处理、小数点后最多4位)
 /// </summary>
 /// <param name="money">金额数目</param>
 /// <returns></returns>        
 public string ConvertToChinese(double? money)
 {
     List<string> cnNums = new List<string> { "", "", "", "", "", "", "", "", "", "" }; //汉字的数字
     var cnIntRadice = new List<string> { "", "", "", "" }; //基本单位
     var cnIntUnits = new List<string> { "", "", "亿", "" }; //对应整数部分扩展单位
     var cnDecUnits = new List<string> { "", "", "", "" }; //对应小数部分单位
     var cnInteger = ""; //整数金额时后面跟的字符
     var cnIntLast = ""; //整型完以后的单位
     double maxNum = 999999999999999.9999; //最大处理的数字
     //decimal maxNum = 999999999999999.9999M; //最大处理的数字【当输入money为decimal时】
     string IntegerNum; //金额整数部分
     string DecimalNum; //金额小数部分
     var ChineseStr = ""; //输出的中文金额字符串
     List<string> parts; //分离金额后用的数组,预定义
     var Symbol = "";//正负值标记
     if (!money.HasValue)
     {
         return "";
     }

     if (money >= maxNum || -money >= maxNum)
     {
         throw new UserFriendlyException("超出最大处理数字");
     }
     if (money == 0)
     {
         ChineseStr = cnNums[0] + cnIntLast + cnInteger;
         return ChineseStr;
     }
     if (money < 0)
     {
         money = -money;
         Symbol = "";
     }

     var moneyStr = money.ToString(); //转换为字符串
     if (moneyStr.IndexOf(".") == -1)
     {
         IntegerNum = moneyStr;
         DecimalNum = "";
     }
     else
     {
         parts = moneyStr.Split(".").ToList();
         IntegerNum = parts[0];
         //DecimalNum = parts[1].Substring(0, 4);
         DecimalNum = parts[1];
         if (DecimalNum.Length > 4)
         {
             DecimalNum = DecimalNum.Substring(0, 4);
         }
     }
     //获取整型部分转换
     if (IntegerNum.ToDecimal() > 0)
     {
         var zeroCount = 0;
         var IntLen = IntegerNum.Length;
         for (var i = 0; i < IntLen; i++)
         {
             var n = IntegerNum.Substring(i, 1);
             var p = IntLen - i - 1;
             var q = p / 4;
             var m = p % 4;
             if (n == "0")
             {
                 zeroCount++;
             }
             else
             {
                 if (zeroCount > 0)
                 {
                     ChineseStr += cnNums[0];
                 }
                 zeroCount = 0; //归零
                 //ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
                 ChineseStr += cnNums[n.ToInt()] + cnIntRadice[m];
             }
             if (m == 0 && zeroCount < 4)
             {
                 ChineseStr += cnIntUnits[q];
             }
         }
         ChineseStr += cnIntLast;
         //整型部分处理完毕
     }
     //小数部分
     if (DecimalNum != "")
     {
         var decLen = DecimalNum.Length;

         for (var i = 0; i < decLen; i++)
         {
             var n = DecimalNum.Substring(i, 1);
             if (n != "0")
             {
                 ChineseStr += cnNums[n.ToInt()] + cnDecUnits[i];
             }
         }
     }
     if (ChineseStr == "")
     {
         ChineseStr += cnNums[0] + cnIntLast + cnInteger;
     }
     else if (DecimalNum == "")
     {
         ChineseStr += cnInteger;
     }
     ChineseStr = Symbol + ChineseStr;
     return ChineseStr;
 }

 

2、C++实现(可处理负值,已验证可用)

#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;

string ConvertMoneyCaps(long double moneySum)
{
    /*------正负值处理 strat-----*/
    string Symble="";//符号标记
    if(moneySum<0)
    {
        moneySum=-moneySum;
        Symble="";    
    }
    /*------正负值处理 end-----*/

    long int temp_i = (long int)moneySum;  /**//* 整数部分 */
    float temp_f = moneySum - temp_i; /**//* 小数部分 */
    int digit = 0, i, j, k, num_i;
    string money("");
    char num[20], *p;
    string name[6] = {"","","","","","亿"};
    string numchar[10] = {"","","","","","","","","",""};
    
    //ltoa(temp_i, num, 10);  /**//* 整数部分转换成字符串后在处理,但是 ltoa不是ANSI(American National Standards Institute) C的标准, 应该避免使用这个函数.使用下边函数替代*/
    snprintf(num, sizeof(num), "%d", temp_i);

    p = num;    
    digit = strlen(num);            /**//* 整数部分位数 */
    /**//*--------处理整数部分 start--------*/
    for(i = 1; i <= digit; i ++)
    {
        k = (digit - i) % 4;
        if(isdigit(*p))
        {
            num_i = *p & 0xF; /* 把字符转换成数字,比如 '0'-> 0,'1' -> 1*/
            /**//*--------转换数字开始---------*/
            if(num_i)
            {
                money = money+ numchar[num_i];
            }
            else
            {
                if(k && (*(p + 1) &0xF))
                    money += "";
            }
            /**//*--------转换数字结束-------*/
            /**//*---------添加计数单位开始----*/
            if(k)
            {
                if(num_i)
                money = money + name[k];
            }
            else
            {
                j = digit - i;
                if(j)
                    money = money + name[j/4 + 3];
                else
                    money += "";
            }
            /**//*--------添加计数单位结束--------*/
            p++;
        }
        else
        {
            money = "遇到非数字退出!";
            return money;
        }
    }
    /**//*--------处理整数部分 End --------*/
    /**//*--------处理小数部分 start--------*/
    if(temp_f > 0.01)
    {
        if((int)(temp_f*10)) money = money + numchar[(int)(temp_f*10)] + "";
        if((int)(temp_f*100)%10) money = money + numchar[(int)(temp_f*100)%10] + "";
    }
    /**//*--------处理小数部分 End--------*/
    money += "";
    money = Symble+ money;
    return money;
}

int main()
{
    long double x = 33.20;
    cout << "please input the money:";
    cin >> x;
    cout << "Convert Money Caps:";
    string money = ConvertMoneyCaps(x);
    cout << money <<endl;
    return 0;
}

 

3、JS实现(可处理负值)

//************************* 把数字金额转换成中文大写数字的函数(可处理负值) *********************************//
function changeNumMoneyToChinese(money)
{
    var cnNums = new Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"); //汉字的数字
    var cnIntRadice = new Array("", "拾", "佰", "仟"); //基本单位
    var cnIntUnits = new Array("", "万", "亿", "兆"); //对应整数部分扩展单位
    var cnDecUnits = new Array("角", "分", "毫", "厘"); //对应小数部分单位
    var cnInteger = "整"; //整数金额时后面跟的字符
    var cnIntLast = "元"; //整型完以后的单位
    var maxNum = 999999999999999.9999; //最大处理的数字
    var IntegerNum; //金额整数部分
    var DecimalNum; //金额小数部分
    var ChineseStr = ""; //输出的中文金额字符串
    var parts; //分离金额后用的数组,预定义    
    var Symbol="";//正负值标记
    if (money == "") {
        return "";
    }

    money = parseFloat(money);
    if (money >= maxNum) {
        alert('超出最大处理数字');
        return "";
    }
    if (money == 0) {
        ChineseStr = cnNums[0] + cnIntLast + cnInteger;
        return ChineseStr;
    }
    if(money<0)
    {
        money=-money;
        Symbol="负 ";        
    }
    money = money.toString(); //转换为字符串
    if (money.indexOf(".") == -1) {
        IntegerNum = money;
        DecimalNum = '';
    } else {
        parts = money.split(".");
        IntegerNum = parts[0];
        DecimalNum = parts[1].substr(0, 4);
    }
    if (parseInt(IntegerNum, 10) > 0) { //获取整型部分转换
        var zeroCount = 0;
        var IntLen = IntegerNum.length;
        for (var i = 0; i < IntLen; i++) {
            var n = IntegerNum.substr(i, 1);
            var p = IntLen - i - 1;
            var q = p / 4;
            var m = p % 4;
            if (n == "0") {
                zeroCount++;
            }
            else {
                if (zeroCount > 0) {
                    ChineseStr += cnNums[0];
                }
                zeroCount = 0; //归零
                ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
            }
            if (m == 0 && zeroCount < 4) {
                ChineseStr += cnIntUnits[q];
            }
        }
        ChineseStr += cnIntLast;
        //整型部分处理完毕
    }
    if (DecimalNum != '') { //小数部分
        var decLen = DecimalNum.length;
        for (var i = 0; i < decLen; i++) {
            var n = DecimalNum.substr(i, 1);
            if (n != '0') {
                ChineseStr += cnNums[Number(n)] + cnDecUnits[i];
            }
        }
    }
    if (ChineseStr == '') {
        ChineseStr += cnNums[0] + cnIntLast + cnInteger;
    } else if (DecimalNum == '') {
        ChineseStr += cnInteger;
    }
    ChineseStr = Symbol +ChineseStr;
    
    return ChineseStr;
}

 

posted @ 2018-06-22 15:45  深海地平线  阅读(641)  评论(0编辑  收藏  举报