- public class Money {
- private static final String[] NUMBERS = { "零", "壹", "贰", "叁", "肆", "伍", "陆","柒", "捌", "玖" };
- private static final String[] IUNIT = { "元", "拾", "佰", "仟", "万", "拾", "佰","仟", "亿", "拾", "佰", "仟"};//最大到千亿
- private static final String[] DUNIT = { "角", "分"};
- public static String toChinese(String str) {
- String integerStr;// 整数部分数字
- String decimalStr;// 小数部分数字
- if (str.indexOf(".") > 0) {
- integerStr = str.substring(0, str.indexOf(".")); //得到整数部分
- decimalStr = str.substring(str.indexOf(".") + 1);//得到小数部分
- }
- else if (str.indexOf(".") == 0) {
- integerStr = "";
- decimalStr = str.substring(1);
- }
- else {
- integerStr = str;
- decimalStr = "";
- }
- if (!integerStr.equals("")) {
- integerStr = Long.toString(Long.parseLong(integerStr));
- if (integerStr.equals("0")) {
- integerStr = "";
- }
- }
- int[] integers = toArray(integerStr); // 整数部分数字
- boolean isWan = isWan(integerStr);// 设置万单位
- int[] decimals = toArray(decimalStr); // 小数部分数字
- return getChineseInteger(integers, isWan) + getChineseDecimal(decimals);
- }
- // 整数部分和小数部分转换为数组,从高位至低位
- private static int[] toArray(String number) {
- int[] array = new int[number.length()];
- for (int i = 0; i < number.length(); i++) {
- array[i] = Integer.parseInt(number.substring(i, i + 1));
- }
- return array;
- }
- //得到中文金额的整数部分。
- private static String getChineseInteger(int[] integers, boolean isWan) {
- StringBuffer chineseInteger = new StringBuffer("");
- int length = integers.length;
- for (int i = 0; i < length; i++) {
- String key = "";
- if (integers[i] == 0) {
- if ((length - i) == 13)
- key = IUNIT[4];
- else if ((length - i) == 9)
- key = IUNIT[8];
- else if ((length - i) == 5 && isWan)
- key = IUNIT[4];
- else if ((length - i) == 1)
- key = IUNIT[0];
- if ((length - i) > 1 && integers[i + 1] != 0)
- key += NUMBERS[0];
- }
- chineseInteger.append(integers[i] == 0 ? key
- : (NUMBERS[integers[i]] + IUNIT[length - i - 1]));
- }
- return chineseInteger.toString();
- }
- //得到中文金额的小数部分。
- private static String getChineseDecimal(int[] decimals) {
- StringBuffer chineseDecimal = new StringBuffer("");
- for (int i = 0; i < decimals.length; i++) {
- if (i == 2)
- break;
- chineseDecimal.append(decimals[i] == 0 ? ""
- : (NUMBERS[decimals[i]] + DUNIT[i]));
- }
- chineseDecimal.toString();
- if(decimals.length==0)//确定"整"的添加情况
- chineseDecimal.append("整");
- else if(decimals.length==1)
- chineseDecimal.append("整");
- else if(decimals.length==2){
- if(decimals[0]==0&&decimals[1]==0||decimals[0]!=0&&decimals[1]==0)
- chineseDecimal.append("整");
- }
- return chineseDecimal.toString();
- }
- //判断第5位数字的单位"万"是否应加。
- private static boolean isWan(String integerStr) {
- int length = integerStr.length();
- if (length > 4) {
- String subInteger = "";
- if (length > 8) {
- subInteger = integerStr.substring(length - 8, length - 4);
- }
- else {
- subInteger = integerStr.substring(0, length - 4);
- }
- return Integer.parseInt(subInteger) > 0;
- }
- else {
- return false;
- }
- }
- public static void main(String[] args) {
- System.out.println("金额转换(最大到千亿,最小到分)");
- String number = "5.00";
- System.out.println(number + " " + Money.toChinese(number));
- number = "123456789123.1";
- System.out.println(number + " " + Money.toChinese(number));
- number = "0.07";
- System.out.println(number + " " + Money.toChinese(number));
- number = "1.90";
- System.out.println(number + " " + Money.toChinese(number));
- number = "1.00";
- System.out.println(number + " " + Money.toChinese(number));
- }
- }
金额转换
最新推荐文章于 2022-10-23 15:09:54 发布