数字转人民币大写

该代码定义了一个名为CommonUtil的工具类,用于获取Java系统的各种属性信息,如Java版本、操作系统信息等,并提供了路径转换方法convertPath,根据操作系统类型调整路径分隔符。此外,类中还有一个方法用于将数字转换为中文大写的人民币格式。
摘要由CSDN通过智能技术生成
package utils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Locale;
import java.util.Properties;

/**
 * 2021/4/26
 * @author wangfei
 */
public class CommonUtil {
    //其他的一些东西,会实用到的时候的
    public static void all() {
        Properties props = System.getProperties();
        System.out.println("Java的执行环境版本号:" + props.getProperty("java.version"));
        System.out.println("Java的执行环境供应商:" + props.getProperty("java.vendor"));
        System.out.println("Java供应商的URL:" + props.getProperty("java.vendor.url"));
        System.out.println("Java的安装路径:" + props.getProperty("java.home"));
        System.out.println("Java的虚拟机规范版本号:" + props.getProperty("java.vm.specification.version"));
        System.out.println("Java的虚拟机规范供应商:" + props.getProperty("java.vm.specification.vendor"));
        System.out.println("Java的虚拟机规范名称:" + props.getProperty("java.vm.specification.name"));
        System.out.println("Java的虚拟机实现版本号:" + props.getProperty("java.vm.version"));
        System.out.println("Java的虚拟机实现供应商:" + props.getProperty("java.vm.vendor"));
        System.out.println("Java的虚拟机实现名称:" + props.getProperty("java.vm.name"));
        System.out.println("Java执行时环境规范版本号:" + props.getProperty("java.specification.version"));
        System.out.println("Java执行时环境规范供应商:" + props.getProperty("java.specification.vender"));
        System.out.println("Java执行时环境规范名称:" + props.getProperty("java.specification.name"));
        System.out.println("Java的类格式版本号号:" + props.getProperty("java.class.version"));
        System.out.println("Java的类路径:" + props.getProperty("java.class.path"));
        System.out.println("载入库时搜索的路径列表:" + props.getProperty("java.library.path"));
        System.out.println("默认的暂时文件路径:" + props.getProperty("java.io.tmpdir"));
        System.out.println("一个或多个扩展文件夹的路径:" + props.getProperty("java.ext.dirs"));
        System.out.println("操作系统的名称:" + props.getProperty("os.name"));
        System.out.println("操作系统的构架:" + props.getProperty("os.arch"));
        System.out.println("操作系统的版本号:" + props.getProperty("os.version"));
        System.out.println("文件分隔符:" + props.getProperty("file.separator"));
        //在 unix 系统中是"/"
        System.out.println("路径分隔符:" + props.getProperty("path.separator"));
        //在 unix 系统中是":"
        System.out.println("行分隔符:" + props.getProperty("line.separator"));
        //在 unix 系统中是"/n"
        System.out.println("用户的账户名称:" + props.getProperty("user.name"));
        System.out.println("用户的主文件夹:" + props.getProperty("user.home"));
        System.out.println("用户的当前工作文件夹:" + props.getProperty("user.dir"));
    }

    /**
     * 根据操作进行进行路径转换
     *
     * @param path
     * @return
     */
    public static String convertPath(String path) {
        Properties props = System.getProperties();
        String osName = props.getProperty("os.name").toUpperCase(Locale.ROOT);
        if (osName.contains("WINDOWS")) {
            System.out.println("true");
            return path.replace("/", "\\");
        } else if (osName.contains("LINUX")) {
            return path.replace("\\", "/");
        }
        throw new TipsException("未识别的操作系统:" + osName);
    }

    /**
     * 得到属性值
     *
     * @param obj
     */
    public static Object readAttributeValue(Object obj,String fieldName) {

        //得到class
        Class cls = obj.getClass();
        //得到所有属性
        Field[] fields = cls.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {//遍历
            try {
                //得到属性
                Field field = fields[i];
                //打开私有访问
                //field.setAccessible(true);
                //获取属性
                String name = field.getName();
                if (name.equals(fieldName)){
                    //获取属性值
                    Object value = field.get(obj);
                    return value;
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return  null;
    }

    /**
     * 获取当前程序执行的基本路径
     *
     * @param path
     * @return
     * @throws IOException
     */
    public static String getCanonicalPath(String path) throws IOException {
        //当前项目下路径
        File file = new File("");
        String pathBash = file.getCanonicalPath() + path;

        return convertPath(pathBash);
    }

    /**
     * 转换为中国人民币大写字符串,精确到分
     *
     * @param money 传入小写数字字符串
     * @return String
     * @throws Exception
     */
    public static String toRMBUpper(String money) {
        boolean lessZero = false;
        //When money is scientific notation
        if (money.contains("E")) {
            BigDecimal bg = new BigDecimal(Double.valueOf(money));
            money = bg.toPlainString();
        }
        if (money.startsWith("-")) {
            money = money.substring(1);
            lessZero = true;
        }

        if (!money.matches("^[0-9]*$|^0+\\.[0-9]+$|^[1-9]+[0-9]*$|^[1-9]+[0-9]*.[0-9]+$")) {
            throw new TipsException("钱数格式错误!");
        }
        String[] part = money.split("\\.");
        String integerData = part[0];
        String decimalData = part.length > 1 ? part[1] : "";
        //Replace front 0
        if (integerData.matches("^0+$")) {
            integerData = "0";
        } else if (integerData.matches("^0+(\\d+)$")) {
            integerData = integerData.replaceAll("^0+(\\d+)$", "$1");
        }

        StringBuffer integer = new StringBuffer();
        for (int i = 0; i < integerData.length(); i++) {
            char perChar = integerData.charAt(i);
            integer.append(upperNumber(perChar));
            integer.append(upperNumber(integerData.length() - i - 1));
        }
        StringBuffer decimal = new StringBuffer();
        if (part.length > 1 && !"00".equals(decimalData)) {
            int length = decimalData.length() >= 2 ? 2 : decimalData.length();
            for (int i = 0; i < length; i++) {
                char perchar = decimalData.charAt(i);
                decimal.append(upperNumber(perchar));
                if (i == 0) {
                    decimal.append('角');
                }
                if (i == 1) {
                    decimal.append('分');
                }

            }
        }
        String result = integer.toString() + decimal.toString();
        result = dispose(result);
        if (lessZero && !"零圆整".equals(result)) {
            result = "负" + result;
        }
        return result;
    }

    private static char upperNumber(char number) {
        switch (number) {
            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';
    }

    private static char upperNumber(int index) {
        int realIndex = index % 9;
        if (index > 8) {
            realIndex = (index - 9) % 8;
            realIndex = realIndex + 1;
        }
        switch (realIndex) {
            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 '亿';
            default:
        }
        return '0';
    }

    private static String dispose(String result) {
        result = result.replaceAll("0", "");
        result = result.replaceAll("零仟零佰零拾|零仟零佰|零佰零拾|零仟|零佰|零拾", "零");
        result = result.replaceAll("零+", "零").replace("零亿", "亿");
        result = result.matches("^.*亿零万[^零]仟.*$") ? result.replace("零万", "零") : result.replace("零万", "万");
        result = result.replace("亿万", "亿");
        //Processing decimals
        result = result.replace("零角", "零").replace("零分", "");
        result = result.replaceAll("(^[零圆]*)(.+$)", "$2");
        result = result.replaceAll("(^.*)([零]+圆)(.+$)", "$1圆零$3");

        //Processing integer
        result = result.replaceAll("圆零角零分|圆零角$|圆$|^零$|圆零$|圆零零$|零圆$", "圆整");
        result = result.replaceAll("^圆整$", "零圆整");


        return result;
    }


    public static void main(String[] args) throws Exception {
        //Test
        System.out.println(toRMBUpper("210.36"));
        //Out: 玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰玖拾玖圆整
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小飞LOVE霞

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

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

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

打赏作者

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

抵扣说明:

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

余额充值