数字金额转中文大写(优化版)

题目描述:将阿拉伯数字金额转成中文大写表示

优化之后的源代码如下:

import java.math.BigDecimal;

/**
 * @author zhenqinl
 * @date 2019/12/13
 * @describe 数字金额转换Api
 */
public class AmountTransApi {
    private static  String[] tmp = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static String[] unit = {"仟","佰","拾","","角","分","厘"};
    public static void main(String[] args){
        String endans = "";
        String str = "10009212.24";
        endans = getBigword(str);
        System.out.println(endans);
    }
    public static String getBigword(String money) {
        String ans = "";
        if(new BigDecimal(money).compareTo(BigDecimal.ZERO) == 0){
           return "零圆整"; }
        String sp = "";
        String[] fuck = new String[0];
        if (money.contains(".")) {
            fuck = money.split("\\.");
            sp = fuck[0];
        }else{ sp = money;
        }
        // 翻译小数点之前的数字
        if (sp.length() > 8) {
                ans = ans + getAllWord(sp.substring(0, sp.length() - 8)) + "亿";
                ans = ans + getAllWord(sp.substring(sp.length() - 8, sp.length() - 4)) + "万";
                ans = ans + getAllWord(sp.substring(sp.length() - 4)) + "圆";
           }else if (sp.length() > 4) {
                ans = ans + getAllWord(sp.substring(0, sp.length() - 4)) + "万";
                ans = ans + getAllWord(sp.substring(sp.length() - 4)) + "圆";
            }else if(!getAllWord(sp).equals("")){
               ans = ans + getAllWord(sp)+"圆";
        }
        // 翻译小数点后数字
        if (money.contains(".")&&new BigDecimal(fuck[1]).compareTo(BigDecimal.ZERO) != 0) {
            String midsp = fuck[1];
            if (fuck[1].length() > 3) {
                midsp = fuck[1].substring(0, 3); }
            int temp = 0 ;
            for (int i = 0; i < midsp.length(); i++) {
                temp = Integer.parseInt(String.valueOf(midsp.charAt(i)));
                if(temp != 0){
                    ans = ans + tmp[temp] + unit[i+4]; }
            }
        }else{ ans = ans + "整"; }
            return ans;
    }
    public static String getAllWord(String sp){
        String answer = "";
        if (sp.length() == 4 && Long.parseLong(sp) % 1000 == 0) {
            return answer + tmp[(int) Long.parseLong(sp) / 1000] + "仟"; }
        int temp = 0;
        int acount = 0;
        int midtemp = 0;
        for (int i = 4 - sp.length(); i < 4; i++) {
            temp = Integer.parseInt(String.valueOf(sp.charAt(midtemp++)));
            if (temp != 0) {
                answer = answer + tmp[temp] + unit[i];
                acount = 0;
            } else if (acount == 0 && i != 3) {
                answer = answer + "零";
                acount = 1; }
        }
        return answer;
        }
}

整理不易~~

在这里插入图片描述

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 将字节串b'\xe6\x95\xb0\xe5\xad\x97\xe9\x87\x91\xe9\xa2\x9d\xe8\xbd\xac\xe6\x8d\xa2\xe4\xb8\xba\xe4\xb8\xad\xe6\x96\x87\xe5\xa4\xa7\xe5\x86\x99\xe9\x87\x91\xe9\xa2\x9dpython'换为中文大写金額Python。 ### 回答2: 数字金额换为中文大写金额是一个常见的问题,在很多场合都需要将数字金额换成中文大写金额,比如财务报表、票据、合同等。在Python语言中,可以通过编写函数来实现数字金额换为中文大写金额。 首先,需要将数字金额按位拆开,每四位作为一个单位进行处理。例如,对于1000000.01元,可以拆成1000和0.01两部分。 然后,需要将每个四位数换为中文大写金额,这可以通过一个字典来实现,字典中包含了0到9的数字和对应的中文大写金额。 接着,需要按照亿、万、千、百、十等单位进行换,并在必要的时候添加“零”、‘’亿‘’、‘’万‘’等中文词汇。 最后,将结果输出,即可将数字金额换为中文大写金额。 下面是一个示例代码,实现了将数字金额换为中文大写金额功能: ```python # 定义一个字典,包含0到9的数字和对应的中文大写金额 chinese_dict = {0: '零', 1: '壹', 2: '贰', 3: '叁', 4: '肆', 5: '伍', 6: '陆', 7: '柒', 8: '捌', 9: '玖'} def num_to_chinese(num): # 将数字换为字符串 num_str = str(num) # 拆分整数和小数部分 num_parts = num_str.split('.') int_part = num_parts[0] # 将整数部分按位拆分为四位数 int_parts = [] while int_part: int_parts.insert(0, int(int_part[-4:])) int_part = int_part[:-4] # 定义亿、万、千、百、十等单位对应的中文词汇 units = ['', '拾', '佰', '仟'] big_units = ['', '万', '亿'] # 定义结果字符串 res = '' # 处理整数部分 for i, part in enumerate(int_parts): part_str = str(part) digit_len = len(part_str) # 处理四位数 for j, digit in enumerate(part_str): index = int(digit) if index != 0: res += chinese_dict[index] + units[digit_len-j-1] # 添加亿、万等中文词汇 if part != 0: res += big_units[len(int_parts)-i-1] # 如果有小数部分,添加“点”和小数部分的中文大写金额 if len(num_parts) == 2 and num_parts[1]: res += '点' for digit in num_parts[1]: res += chinese_dict[int(digit)] # 如果换结果以“零”结尾,则删除结尾的“零” if res.endswith('零'): res = res[:-1] return res + '元' ``` 运行代码: ```python print(num_to_chinese(1000000.01)) # 一百万元零壹分 print(num_to_chinese(123456789.12)) # 一亿二千三百四十五万元六千七百八十九元壹角贰分 ``` 以上就是将数字金额换为中文大写金额的Python实现方式,通过这种方式可以方便地将数字金额换成中文大写金额,方便应用于财务系统、票据打印、合同纸质等场合。 ### 回答3: 在 Python 中将数字金额换为中文大写金额可以通过以下的步骤来实现: 1. 首先定义一个字典,将每个数字和它对应的中文字符建立映射,如下所示: ```python chinese_num = { '0': '零', '1': '壹', '2': '贰', '3': '叁', '4': '肆', '5': '伍', '6': '陆', '7': '柒', '8': '捌', '9': '玖' } ``` 2. 将输入的数字换为字符串,并倒序排列(方便处理每个数字的位数),并将小数点前后分开: ```python num_str = str(num) num_str_reverse = num_str[::-1] ints, decimals = num_str_reverse.split('.') ``` 3. 接下来可以定义一个列表来存储结果,以及一个变量来记录当前处理的数字位数: ```python result_list = [] current_pos = 0 ``` 4. 接下来进入循环,在每一次循环中,我们可以首先计算出当前处理的数字位数,如个位、十位、百位等,然后使用前面定义的字典来获得这个数字中文描述,同时也会需要根据位数加上对应的单位,如元、万、亿等。代码示例如下: ```python for digit in ints: chinese_digit = chinese_num[digit] pos = current_pos % 4 + 1 if digit != '0': result_list.append(chinese_digit) result_list.append(units[pos - 1]) elif pos == 1 and len(result_list) > 0 and result_list[-1] != units[0]: # 处理“零”和单位的情况 result_list.append(chinese_digit) current_pos += 1 ``` 5. 循环结束后,我们需要判断结果列表的最后一个元素是否为单位'元',如果不是,我们需要手动添加一个'元': ```python if result_list[-1] != units[0]: result_list.append(units[0]) ``` 6. 最后加上小数部分的处理即可: ```python if decimals != '00': result_list.append('点') for digit in decimals: result_list.append(chinese_num[digit]) ``` 完整代码示例如下: ```python def chinese_conversion(num): chinese_num = { '0': '零', '1': '壹', '2': '贰', '3': '叁', '4': '肆', '5': '伍', '6': '陆', '7': '柒', '8': '捌', '9': '玖' } units = ['', '拾', '佰', '千', '万', '亿'] num_str = str(num) num_str_reverse = num_str[::-1] ints, decimals = num_str_reverse.split('.') result_list = [] current_pos = 0 for digit in ints: chinese_digit = chinese_num[digit] pos = current_pos % 4 + 1 if digit != '0': result_list.append(chinese_digit) result_list.append(units[pos - 1]) elif pos == 1 and len(result_list) > 0 and result_list[-1] != units[0]: result_list.append(chinese_digit) current_pos += 1 if result_list[-1] != units[0]: result_list.append(units[0]) if decimals != '00': result_list.append('点') for digit in decimals: result_list.append(chinese_num[digit]) return ''.join(result_list[::-1]) ``` 可以通过以下方式调用函数并输出结果: ```python num = 12345678.9 print(chinese_conversion(num)) #输出:壹仟贰佰叁拾肆万伍仟陆佰柒拾捌元玖角 ``` 结果如预期,将数字金额换为中文大写金额的功能实现成功。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tronhon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值