• 将万亿以下的阿拉伯数字转为中文金额


    package test.practice.month3;

    public class Test005 {
    //可以不用swich case将123456789转为一二三四五六七八九
    //直接用char[] chars= {'一','二','三','四','五','六','七','八','九'};

    public static void main(String[] args) {
    System.out.println(getCMoney(102030405067L));
    }
    private static String getCMoney(long n) {
    String result="";
    int i=new Long(n/100000000).intValue();
    if(i!=0) {
    result=result+getThousand(i)+'亿';
    }
    i=new Long((n%100000000)/10000).intValue();
    if(i!=0) {
    result=result+getThousand(i)+'万';
    }else {
    result=result+'零';
    }
    i=new Long(n%10000).intValue();
    if(i!=0) {
    result=result+getThousand(i);
    }
    result+="元整";
    if(result.charAt(0)=='零') {
    result=result.substring(1);
    }
    return result;
    }
    private static String getThousand(int n) {
    String result="";
    int i=n/1000;
    if(i!=0) {
    result=result+getChar(i)+'仟';
    }
    i=(n%1000)/100;
    if(i!=0) {
    result=result+getChar(i)+'佰';
    }
    else {
    result=result+'零';
    }
    i=(n%100)/10;
    if(i!=0) {
    result=result+getChar(i)+'拾';
    }
    else {
    result=result+'零';
    }
    i=n%10;
    if(i!=0) {
    result=result+getChar(i);
    }
    result=result.replace("零零", "零");
    if(result.charAt(0)=='零') {
    result=result.substring(1);
    }
    if(result.charAt(result.length()-1)=='零') {
    result=result.substring(0, result.length()-1);
    }
    return result;
    }
    private static char getChar(int i) {
    char c;
    switch(i) {
    case 1:
    c='壹';
    break;
    case 2:
    c='贰';
    break;
    case 3:
    c='叁';
    break;
    case 4:
    c='肆';
    break;
    case 5:
    c='伍';
    break;
    case 6:
    c='陆';
    break;
    case 7:
    c='柒';
    break;
    case 8:
    c='捌';
    break;
    case 9:
    c='玖';
    break;
    default:c='越';
    }
    return c;
    }
    }

    网上找来的别人的更好的方法

    public class Test007 {
    private static final char[] UNIT = "万千佰拾亿千佰拾万千佰拾元角分".toCharArray();
    private static final char[] DIGIT = "零壹贰叁肆伍陆柒捌玖".toCharArray();
    private static final double MAX_VALUE = 9999999999999.99D;

    public static String change(double v) {
    if (v < 0 || v > MAX_VALUE) {
    return "参数非法!";
    }
    long l =(long)(v*100);
    if (l == 0) {
    return "零元整";
    }
    String strValue = l + "";
    // j用来控制单位
    int j = UNIT.length - strValue.length();
    String rs = "";
    boolean isZero = false;
    for (int i = 0;i < strValue.length(); i++, j++) {
    char ch = strValue.charAt(i);
    if (ch == '0') {
    isZero = true;
    if (UNIT[j] == '亿' || UNIT[j] == '万' || UNIT[j] == '元') {
    rs = rs + UNIT[j];
    isZero = false;
    }
    } else {
    if (isZero) {
    rs = rs + "零";
    isZero = false;
    }
    rs = rs + DIGIT[ch - '0'] + UNIT[j];
    }
    }
    if (!rs.endsWith("分")&&!rs.endsWith("角")) {
    rs = rs + "整";
    }
    rs = rs.replaceAll("亿万", "亿");
    rs = rs.replaceAll("^壹拾", "拾");
    return rs;
    }
    public static void main(String[] args) {
    System.out.println(Test007.change(9999999999999.90D));
    }
    }

  • 相关阅读:
    算法训练 数位分离
    算法训练 薪水计算
    算法训练 整除问题
    算法训练 数对
    pom.xml一个简单配置
    MyBatis3.4.0以上的分页插件错误:Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named prepare. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.stateme
    MyBatis3-实现MyBatis分页
    mybatis 易百练习笔记
    maven pom.xml配置
    Description Resource Path Location Type The superclass "javax.servlet.http.HttpServlet" was not foun
  • 原文地址:https://www.cnblogs.com/yuezeyuan/p/7754530.html
Copyright © 2020-2023  润新知