zoukankan      html  css  js  c++  java
  • 阿拉伯数字转中文数字

    package algorithm.other;

    /**
    * 阿拉伯数字转中文数字
    * @author CEMABENTENG
    *
    */
    public class ChineseNum
    {
    private static String[] chnNumChar =
    { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };

    private static String[] chnUnitSection =
    { "", "万", "亿", "万" };

    private static String[] chnUnitChar =
    { "", "十", "百", "千" };

    public static void main(String[] args)
    {
    System.out.println(numToChn(0));
    System.out.println(numToChn(1000021000));
    System.out.println(numToChn(10));
    System.out.println(numToChn(11));
    System.out.println(numToChn(100));
    System.out.println(numToChn(101));
    System.out.println(numToChn(203));
    System.out.println(numToChn(1001));
    System.out.println(numToChn(1210121003));
    for (int i = 0; i < 10; i++)
    {
    long a = (long) Math.floor(Math.random() * 999900000000l);
    System.out.println(a + "~~~~~~~" + numToChn(a));
    }
    }

    /**
    * 阿拉伯数字转中文数字
    * @param num 一万亿以内正整数
    * @return
    */
    public static String numToChn(long num)
    {
    String chnStr = "";
    String strIns = "";
    int unitPos = 0;
    boolean zero = false;
    while (num > 0)
    {
    int section = (int) (num % 10000);
    if (zero)
    {
    chnStr = chnNumChar[0] + chnStr;
    }
    strIns = SectionToChn(section);

    strIns += (section != 0) ? chnUnitSection[unitPos] : chnUnitSection[0];
    chnStr = strIns + chnStr;
    zero = (section < 1000) && (section > 0);
    num = num / 10000;
    unitPos++;
    }

    if (chnStr.length() == 0)
    {
    return chnNumChar[0];
    }

    return chnStr;
    }

    private static String SectionToChn(int section)
    {
    String x = "";
    String strIns;
    int unitPos = 0;//位数
    boolean zero = true;
    while (section > 0)
    {
    int v = section % 10;
    if (v == 0)
    {
    if ((section == 0) || !zero)
    {
    zero = true;
    x = chnNumChar[v] + x;//补零
    }
    } else
    {
    zero = false;
    strIns = chnNumChar[v];
    strIns += chnUnitChar[unitPos];
    //去一
    if (section < 10 && strIns.equals(chnNumChar[1] + chnUnitChar[1]))
    {
    strIns = strIns.substring(1);
    }
    x = strIns + x;
    }
    unitPos++;
    section = section / 10;
    }
    return x;
    }
    }

  • 相关阅读:
    OOm是否可以try catch ?
    Java中两个线程是否可以同时访问同一个对象的两个不同的synchronized方法?
    PHP请求ws出现的问题
    AndFix注意事项
    thinkphp 查表返回的数组,js解析有UNICode编码,解决办法
    thinkphp用ajax遇到的坑——ajax请求没有反应
    用php获取js变量的值
    android项目安装报错:INSTALL_FAILED_CONFLICTING_PROVIDER
    96
    wuti
  • 原文地址:https://www.cnblogs.com/uip001/p/6872930.html
Copyright © 2011-2022 走看看