中文转换大小写工具包(ChineseCapitalNumber)

package com.baiwang.zbxjf.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class ChineseCapitalNumber {
    public static String[] chineseDigits = new String[]{
            "\u96f6", "\u58f9", "\u8d30", "\u53c1", "\u8086", "\u4f0d", "\u9646", "\u67d2",
            "\u634c", "\u7396"};

    private static final BigDecimal MAX = new BigDecimal(99999999999999.99d);
    private static final BigDecimal MIN = new BigDecimal(-99999999999999.99d);
    private static final BigDecimal big100 = new BigDecimal(100);
    private static final BigDecimal big10000 = new BigDecimal(10000);

    /**
     * Convers amount number into Chinese capital number, keeps 2 digits of decimal part by round
     *
     * @param sAmount
     * @return Capital Number in Chinese
     */
    public static String amount2Chinese(String sAmount) {
        BigDecimal amount = new BigDecimal(sAmount);
        if (amount.compareTo(MAX) > 0 || amount.compareTo(MIN) < 0)
            throw new IllegalArgumentException(
                    "parameter overflows (-99999999999999.99 ~ 99999999999999.99)!");

        boolean negative = false;
        if (amount.compareTo(BigDecimal.ZERO) < 0) {
            negative = true;
            amount = amount.negate();
        }

        BigDecimal temp = amount.multiply(big100).setScale(0, RoundingMode.HALF_UP);
        int numFen = temp.remainder(BigDecimal.TEN).intValue(); // cent
        temp = temp.divideToIntegralValue(BigDecimal.TEN);
        int numJiao = temp.remainder(BigDecimal.TEN).intValue(); // dime
        temp = temp.divideToIntegralValue(BigDecimal.TEN);
        //temp is the integer part
        int[] parts = new int[20];
        int numParts = 0;
        for (int i = 0; ; i++) {
            if (temp.compareTo(BigDecimal.ZERO) == 0)
                break;
            int part = temp.remainder(big10000).intValue();
            parts[i] = part;
            numParts++;
            temp = temp.divideToIntegralValue(big10000);
        }

        boolean beforeWanIsZero = true; // 10 thousand

        String chineseStr = "";
        for (int i = 0; i < numParts; i++) {

            String partChinese = partTranslate(parts[i]);
            if (i % 2 == 0) {
                if ("".equals(partChinese))
                    beforeWanIsZero = true;
                else
                    beforeWanIsZero = false;
            }

            if (i != 0) {
                if (i % 2 == 0)
                    chineseStr = "\u4ebf" + chineseStr;
                else {
                    if ("".equals(partChinese) && !beforeWanIsZero)
                        chineseStr = "\u96f6" + chineseStr;
                    else {
                        if (parts[i - 1] < 1000 && parts[i - 1] > 0)
                            chineseStr = "\u96f6" + chineseStr;
                        chineseStr = "\u4e07" + chineseStr;
                    }
                }
            }
            chineseStr = partChinese + chineseStr;
        }

        if ("".equals(chineseStr))
            chineseStr = chineseDigits[0];
        else if (negative)
            chineseStr = "\u8d1f" + chineseStr;

        chineseStr = chineseStr + "\u5143";

        if (numFen == 0 && numJiao == 0) {
            chineseStr = chineseStr + "\u6574";
        } else if (numFen == 0) {
            chineseStr = chineseStr + chineseDigits[numJiao] + "\u89d2";
        } else {
            if (numJiao == 0)
                chineseStr = chineseStr + "\u96f6" + chineseDigits[numFen] + "\u5206";
            else
                chineseStr = chineseStr + chineseDigits[numJiao] + "\u89d2"
                        + chineseDigits[numFen] + "\u5206";
        }

        return chineseStr;


    }

    /**
     * Turns a integer which is within range 0~9999 into Chinese capital number,return "" when it is 0
     *
     * @param amountPart
     * @return
     */
    private static String partTranslate(int amountPart) {

        if (amountPart < 0 || amountPart > 10000) {
            throw new IllegalArgumentException("parameter has to be an integer between 0 and 10000");
        }

        String[] units = new String[]{"", "\u62fe", "\u4f70", "\u4edf"};

        int temp = amountPart;

        String amountStr = new Integer(amountPart).toString();
        int amountStrLength = amountStr.length();
        boolean lastIsZero = true;
        String chineseStr = "";

        for (int i = 0; i < amountStrLength; i++) {
            if (temp == 0)
                break;
            int digit = temp % 10;
            if (digit == 0) {
                if (!lastIsZero)
                    chineseStr = "\u96f6" + chineseStr;
                lastIsZero = true;
            } else {
                chineseStr = chineseDigits[digit] + units[i] + chineseStr;
                lastIsZero = false;
            }
            temp = temp / 10;
        }
        return chineseStr;
    }
   
    public static void main(String args[]) throws UnsupportedEncodingException{
  System.out.println(amount2Chinese("123134"));
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值