package com.willalp.common.utils;

import org.springframework.stereotype.Service;


@Service
public class PrintUtil {
    /**
     * 人民币转大写
     *
     * @param amount
     * @return
     */
    public static String changeMoneyUpper(Object amount) {
        if (amount == null || amount.toString().length() == 0)
            return null;
        String sNumber = amount.toString();
        String[] oneUnit = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟"};
        String[] twoUnit = {"分", "角"};
        String[] sChinese = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        String sign = ""; // 符号位 add by Zhangxw
        if (sNumber.startsWith("-")) {
            sNumber = sNumber.substring(1);
            sign = "负";
        }
        int pointPos = sNumber.indexOf("."); // 小数点的位置
        String sInteger;// 记录整数部分
        String sDecimal;// 记录小数部分
        String value = "";// 记录返回结果
        if (pointPos != -1) {
            // 分解并记录整数部分，注意substring()的用法
            sInteger = sNumber.substring(0, pointPos); // 分解并记录小数部分
            sDecimal = sNumber.substring(pointPos + 1,
                    pointPos + 3 < sNumber.length() ? pointPos + 3 : sNumber.length());
        } else { // 没有小数部分的情况
            sInteger = sNumber;
            sDecimal = "";
        } // 格式化整数部分，并记录到返回结果
        for (int i = 0; i < sInteger.length(); i++) {
            int temp = Integer.parseInt(sInteger.substring(i, i + 1));
            value += sChinese[temp] + oneUnit[sInteger.length() - i - 1];
        } // 格式化小数部分，并记录到返回结果
        for (int i = 0; i < sDecimal.length(); i++) {
            int temp = Integer.parseInt(sDecimal.substring(i, i + 1));
            // value += sChinese[temp] + twoUnit[sDecimal.length() - i - 1];
            value += sChinese[temp] + twoUnit[2 - i - 1];
        }
        return sign + value;
    }

}
