【java工具类】小数、整数转中文小写

小数、整数转中文小写

1、可转化带小数的数字,成为小写中文数字

2、代码如下

java 复制代码
public class MoneyToChineseUtil {

    private static final String[] CHINESE_NUMS = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private static final String[] UNITS = {"", "十", "百", "千"};
    private static final String[] BIG_UNITS = {"", "万", "亿", "万亿"};
    private static final String[] DECIMAL_UNITS = {"毛", "分", "厘"}; // 对应小数后三位

    /**
     * 金额转中文小写
     * @param amount 任意金额
     * @return 中文金额字符串
     */
    public static String toChineseMoney(double amount) {
        if (amount == 0) return "零元";
        
        // 1. 格式化为三位小数,防止浮点数精度干扰
        String strAmount = String.format("%.3f", amount);
        String[] parts = strAmount.split("\\.");
        
        long integerPart = Long.parseLong(parts[0]);
        String decimalPart = parts[1];

        StringBuilder result = new StringBuilder();

        // 2. 处理整数部分(元)
        if (integerPart > 0) {
            result.append(integerToChinese(integerPart)).append("元");
        } else if (Long.parseLong(decimalPart) > 0) {
            // 整数部分为0但小数部分有值,通常读作"零元..."或直接开始
            result.append("零元");
        }

        // 3. 处理小数部分(毛、分、厘)
        boolean hasDecimal = false;
        for (int i = 0; i < decimalPart.length(); i++) {
            int digit = decimalPart.charAt(i) - '0';
            if (digit > 0) {
                result.append(CHINESE_NUMS[digit]).append(DECIMAL_UNITS[i]);
                hasDecimal = true;
            } else if (integerPart > 0 && i < 2 && (decimalPart.charAt(i+1) - '0') > 0) {
                // 如果是"12.06",中间的0需要读出来
                if (!result.toString().endsWith("零")) {
                    result.append("零");
                }
            }
        }

        if (!hasDecimal && integerPart > 0) {
            result.append("整");
        }

        return result.toString()
                .replaceAll("零+", "零")
                .replaceAll("零元", "零") // 根据习惯微调
                .replaceFirst("^零", "");
    }

    private static String integerToChinese(long num) {
        StringBuilder sb = new StringBuilder();
        int unitIndex = 0;
        while (num > 0) {
            int section = (int) (num % 10000);
            if (section != 0) {
                String s = sectionToChinese(section);
                sb.insert(0, s + BIG_UNITS[unitIndex]);
            }
            num /= 10000;
            unitIndex++;
        }
        return sb.toString().replaceAll("^一十", "十");
    }

    private static String sectionToChinese(int section) {
        StringBuilder sb = new StringBuilder();
        boolean zeroFlag = true;
        for (int i = 0; i < 4; i++) {
            int digit = section % 10;
            if (digit == 0) {
                if (!zeroFlag) {
                    zeroFlag = true;
                    sb.insert(0, CHINESE_NUMS[0]);
                }
            } else {
                zeroFlag = false;
                sb.insert(0, CHINESE_NUMS[digit] + UNITS[i]);
            }
            section /= 10;
        }
        return sb.toString();
    }

    public static void main(String[] args) {
      System.out.println(toChineseMoney(12.56));      // 十二元五毛六分
        System.out.println(toChineseMoney(105.02));     // 一百零五元零二分
        System.out.println(toChineseMoney(0.508));      // 五毛八厘
        System.out.println(toChineseMoney(1000.00));    // 一千元整
        System.out.println(toChineseMoney(10.103));     // 十元一毛三厘
        System.out.println(toChineseMoney(8206010.654));     // 十元一毛三厘

    }
}
相关推荐
练习时长一年9 小时前
@SneakyThrows注解
开发语言
我的xiaodoujiao9 小时前
快速学习Python基础知识详细图文教程14--模块
开发语言·python·学习·测试工具
NoteStream9 小时前
【c语言基础】C语言常见概念
c语言·开发语言·编辑器
吃好睡好便好9 小时前
MATLAB中图像的线性变换
开发语言·图像处理·学习·计算机视觉·matlab
charlie1145141919 小时前
Cinux —— 给物理内存建账本:bitmap 物理内存管理器
开发语言·c++·操作系统·开源项目
ShiXZ2139 小时前
Java 8 Stream API 实用技巧详解:从入门到精通
java·开发语言
冻柠檬飞冰走茶9 小时前
PTA基础编程题目集 7-27 冒泡法排序(C语言实现)
c语言·开发语言·数据结构·算法
听雨入夜10 小时前
“同声传译”还是“全文翻译”?为何HotSpot虚拟机仍要保留解释器?
开发语言·python
随风M记忆s10 小时前
GEE&Python-demo:利用Sentinel-监测北京奥林匹克森林公园年NDVI变化(附Python版)
开发语言·python·sentinel
C++、Java和Python的菜鸟10 小时前
第9章 后端Web进阶(AOP)
java·开发语言·前端