人民币大写转换工具C++实现(支持任意位金额转换)

人民币大写转换规则

中文大写金额数字应用正楷或行书填写,如壹、贰、叁、肆、伍、陆、柒、捌、玖、拾、佰、仟、万、亿、元、角、分、零、整(正)等字样。不得用一、二(两)、三、四、五、六、七、八、九、十、廿、毛、另(或0)填写,不得自造简化字。如果金额数字书写中使用繁体字,如贰、陆、亿、万、圆的,也可。

中文大写金额数字到"元"为止的,在"元"之后,应写"整"(或"正")字,在"角"之后,可以不写"整"(或"正")字。大写金额数字有"分"的,“分"后面不写"整”(或"正")字。

中文大写金额数字前应标明"人民币"字样,大写金额数字有"分"的,“分"后面不写"整”(或"正")字。

中文大写金额数字前应标明"人民币"字样,大写金额数字应紧接"人民币"字样填写,不得留有空白。大写金额数字前未印"人民币"字样的,应加填"人民币"三字。在票据和结算凭证大写金额栏内不得预印固定的"仟、佰、拾、万、仟、佰、拾、元、角、分"字样。

人民币大写转换工具(支持任意位金额转换)

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

/*
案例:
1234.09
0.00
0.09
1002.03
01002.4032
120000.34
12000000000000003400000056 
*/

//检查字符串合法性
bool judge_isnum(string input){
    bool is_num = true;
    if(input[0] = '-')
        input = input.erase(0, 1);
    for (int i = 0; i < input.length(); i++){
        if (input[i] == '.'){
            input = input.erase(i, 1);
            break;
        }
    }
    for (int i = 0; i < input.length(); i++){
        if (input[i] < '0' || input[i] > '9'){
            is_num = false;
            break;
        }
    }
    return is_num;
}

// 将数字转换为大写
string get_c(int c){
    switch(c){
    case 0: return "零";
    case 1: return "壹";
    case 2: return "贰";
    case 3: return "叁";
    case 4: return "肆";
    case 5: return "伍";
    case 6: return "陆";
    case 7: return "柒";
    case 8: return "捌";
    case 9: return "玖";
    default: return "";
    }
}

// 判断一个字符串是否全0
bool judge_zero(string num){
    bool zero = true;
    for (int i = 0; i < num.length(); i++){
        if (num[i] != '0'){
            zero = false;
            break;
        }
    }
    return zero;
}

// 字符串转换为数字
int str_to_num(string str){
    int num ;
    stringstream ss;  
    ss << str;                  
    ss >> num;
    return num;
}

// 数字转换为字符串
string num_to_str(int num){
    string str;
    stringstream ss;
    ss << num;
    ss >> str;
    return str;
}

// 分离出小数点前面部分
string get_pre(string input){
    string pre = "";
    int i = 0;
    while (i < input.length() && input[i] != '.'){
        pre += input[i];
        i++;
    }
    return pre;
}

// 分离出小数点后面部分
string get_post(string input){
    int i = 0;
    string post = "";
    while (i < input.length() && input[i] != '.'){
        i++;
    }
    if (i < input.length()){
        i++;
        while (i < input.length()){
            post += input[i];
            i++;
        }
    }
    return post;
}

// 四位及以下数字转换为大写
string get_four(int num){
    string out = "";
    if (num == 0)
        out = "";
    else if (num < 10){
        out = get_c(num);
    }
    else if(num < 100){
        if (num % 10 == 0)
            out = get_four(num / 10) + "拾";
        else 
            out = get_four(num / 10) + "拾" + get_four(num % 10);
    }
    else if(num < 1000){
        if (num % 100 == 0)
            out = get_four(num / 100) + "佰";
        else if(num % 100 < 10)
            out = get_four(num / 100) + "佰零" + get_four(num % 100);
        else
            out = get_four(num / 100) + "佰" + get_four(num % 100);
            
    }
    else if(num < 10000){
        if (num % 1000 == 0)
            out = get_four(num / 1000) + "仟";
        else if(num % 1000 < 100)
            out = get_four(num / 1000) + "仟零" + get_four(num % 1000);
        else
            out = get_four(num / 1000) + "仟" + get_four(num % 1000);
    }
    return out;
}

//八位数及以下数字转换为大写
string get_eight(string num){
    while (num[0] == '0') // 删除开头的0
        num = num.erase(0, 1);
    if (num.length() <= 4)
        return get_four(str_to_num(num));
    string out;
    string str1 = num.substr(0, num.length() - 4);
    string str2 = num.erase(0, num.length() - 4);
    int num1 = str_to_num(str1);
    int num2 = str_to_num(str2);
    if (str2[0] == '0' && !judge_zero(str2))
        out = get_four(num1) + "万零" + get_four(num2);
    else if (judge_zero(str2))
        out = get_four(num1) + "万";
    else
        out = get_four(num1) + "万" + get_four(num2);
    return out;
}

// 十六位数及以下数字转换为大写
string get_16(string num){
    while (num[0] == '0') // 删除开头的0
        num = num.erase(0, 1);
    if (num.length() <= 8)
        return get_eight(num);
    string out;
    string str1 = num.substr(0, num.length() - 8);
    string str2 = num.erase(0, num.length() - 8);
    if (str2[0] == '0' && !judge_zero(str2))
        out = get_eight(str1) + "亿零" + get_eight(str2);
    else if (judge_zero(str2))
        out = get_eight(str1) + "亿";
    else 
        out = get_eight(str1) + "亿" + get_eight(str2);
    return out;
}

// 十六位数以上数字转换为大写,递归调用
string get_out_16(string num){
    while (num[0] == '0') // 删除开头的0
        num = num.erase(0, 1);
    if (num.length() <= 16)
        return get_16(num);
    string out;
    while (num.length() > 16){
        string str1 = num.substr(0, num.length() - 8);
        string str2 = num.erase(0, num.length() - 8);
        if (str2[0] == '0' && !judge_zero(str2))
            out = get_out_16(str1) + "亿零" + get_eight(str2);
        else if (judge_zero(str2))
            out = get_out_16(str1) + "亿";
        else 
            out = get_out_16(str1) + "亿" + get_eight(str2);
    }
    return out;
}


// 整数部分转换为大写
string get_pre_c(string pre){
    string out1 = "";
    if (pre.length() <= 4){
        int num = str_to_num(pre);
        out1 = get_four(num);
    }
    else if (pre.length() <= 8)
        out1 = get_eight(pre);
    else if (pre.length() <= 16)
        out1 = get_16(pre);
    else
        out1 = get_out_16(pre);
    return out1;
}


// 小数部分转换为大写
string get_post_c(string post, int len){
    string out2;
    int num;
    if (post.length() == 0)
        out2 = "";
    else if (post.length() == 1){
        num = str_to_num(post);
        if (num == 0)
            out2 = "";
        else
            out2 = post + "角";
    }
    else{ // 字符串长度>=2,只取前两个
        string two = post.substr(0, 2);
        num = str_to_num(two);

        if (num == 0){
            out2 = "";
        }
        else if (num < 10 && len != 0)
            out2 = "零" + get_c(num) + "分";
        else if (num < 10 && len == 0) // 整数部分为0,分前面不需要0
            out2 = get_c(num) + "分";
        else{
            int jiao = num / 10;
            int fen = num % 10;
            if (fen == 0)
                out2 = get_c(jiao) + "角";
            else
                out2 = get_c(jiao) + "角" + get_c(fen) + "分";
        }
    }
    return out2;
}


int main(){
    string input;
    cout << "请输入需要转换的金额(输入负数退出): ";
    cin >> input;
    while(!judge_isnum(input)){
        cout << "不合法,请重新输入: ";
        cin >> input;
    }

    while (input[0] != '-'){
        while (input[0] == '0') // 删除开头的0
            input = input.erase(0, 1);
        string pre = get_pre(input);
        string post = get_post(input);
        
        string out1 = get_pre_c(pre);
        string out2 = get_post_c(post, pre.length());

        string output = "";
        if (out1.length() == 0 && out2.length() == 0)
            output = "人民币零元整";
        else if(out1.length() == 0)
            output = "人民币" + out2;
        else if(out2.length() == 0)
            output = "人民币" + out1 + "元整";
        else
            output = "人民币" + out1 + "元" + out2;
        cout << "转换后大写为: " << output << endl;
        
        cout << endl;
        cout << "请输入需要转换的金额(输入负数退出): ";
        cin >> input;
        while(!judge_isnum(input)){
            cout << "不合法,请重新输入: ";
            cin >> input;
        }
    }
    return 0;
}

  • 10
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是Java实现人民币金额转换大写中文的完整源码: ```java import java.math.BigDecimal; public class RMBAmountToChinese { private static final String[] CN_NUM = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; // 中文数字 private static final String[] CN_UNIT = {"", "拾", "佰", "仟", "万", "亿"}; // 中文单 private static final BigDecimal HUNDRED = new BigDecimal("100"); // 100 private static final BigDecimal TEN_THOUSAND = new BigDecimal("10000"); // 10000 private static final BigDecimal ONE_HUNDRED_MILLION = new BigDecimal("100000000"); // 1亿 /** * 将人民币金额转换大写中文 * @param amount 人民币金额,最大支持1亿 * @return 大写中文金额 */ public static String rmbAmountToChinese(BigDecimal amount) { StringBuilder sb = new StringBuilder(); amount = amount.setScale(2, BigDecimal.ROUND_HALF_UP); // 保留2小数并四舍五入 if (amount.compareTo(ONE_HUNDRED_MILLION) >= 0) { // 1亿及以上 BigDecimal quotient = amount.divide(ONE_HUNDRED_MILLION, BigDecimal.ROUND_DOWN); // 商 BigDecimal remainder = amount.remainder(ONE_HUNDRED_MILLION); // 余数 sb.append(convert(quotient)).append("亿"); if (remainder.compareTo(BigDecimal.ZERO) > 0) { sb.append(convert(remainder)); } } else { // 1亿以下 sb.append(convert(amount)); } return sb.toString(); } // 将数字转换大写中文 private static String convert(BigDecimal num) { StringBuilder sb = new StringBuilder(); String str = num.toString(); int index = str.indexOf("."); // 小数点置 if (index == -1) { // 整数 index = str.length(); } String integerPart = str.substring(0, index); // 整数部分 String decimalPart = str.substring(index + 1); // 小数部分 int length = integerPart.length(); boolean zero = false; // 是否有过连续的零 for (int i = 0; i < length; i++) { int digit = integerPart.charAt(i) - '0'; // 当前数字 if (digit == 0) { // 当前数字是0 if (!zero && length - i < CN_UNIT.length) { // 如果前面没有连续的零,并且数字不是在万或亿的单,则加上零 sb.append(CN_NUM[digit]); zero = true; } } else { // 当前数字不是0 if (zero) { // 如果前面有连续的零,则加上零 sb.append(CN_NUM[0]); zero = false; } sb.append(CN_NUM[digit]).append(CN_UNIT[length - i - 1]); } } if (decimalPart.equals("00")) { // 没有小数部分 sb.append("整"); } else { // 有小数部分 sb.append(CN_NUM[decimalPart.charAt(0) - '0']).append("角"); if (decimalPart.length() > 1 && decimalPart.charAt(1) != '0') { sb.append(CN_NUM[decimalPart.charAt(1) - '0']).append("分"); } } return sb.toString(); } public static void main(String[] args) { BigDecimal amount = new BigDecimal("1234567.89"); // 人民币金额 String chineseAmount = rmbAmountToChinese(amount); // 转换成中文金额 System.out.println(chineseAmount); // 输出结果:壹佰贰十三万肆仟伍佰陆拾柒元捌角玖分 } } ``` 通过以上代码,我们可以将一个人民币金额转换大写的中文金额。其中,我们使用了BigDecimal类进行精度计算,同时使用了StringBuilder类进行字符串拼接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值