package com.kangdy.test; /** * 数字金额转化成大写 * 先将整数与小数部分分开,计算小数部分,角分并保存 * 整数部分长度不足12位,前面加0补足。 * 将整数部分分割3部分。高4位代表亿,中间的是万,其余分别代表千,百,十,个 * 定一个方法拼出每一部分串。 * 最后整数与小数部分合成。 * @author dkang * */ public class NumberToString {
String numberStr;
public static final String unit[] = { "", "十", "百", "千", "万", "亿" };
public static final String unit2[] = { "元", "角", "分" };
public static final String numStr[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
/** * 字符串长度不足12位用0补足 * * @param str * @return */ private String additionalZero(String str) { StringBuffer strb = new StringBuffer(); if (str.length() < 12) { int size = 12 - str.length(); for (int i = 0; i < size; i++) { strb.append("0"); } } return strb.append(str).toString(); }
/** * 递归拆分数字成字符串 * * @param value * @param strBuffer * @return */ private String decomposeNumberToString(int value, StringBuffer strBuffer) { int quotient = 0; quotient = value / 10; if (quotient != 0) { decomposeNumberToString(quotient, strBuffer); } int remaider = value % 10; strBuffer.append(remaider + ","); return strBuffer.toString().substring(0, strBuffer.toString().length() - 1); }
/** * 使用循环拆分数字成字符串 * * @param value * @return */ private String decomposeNumberToString2(int value) { StringBuilder strBuilder = new StringBuilder(); int quotient = value; int remaider = 0; while (quotient != 0) { remaider = quotient % 10; strBuilder.append(remaider + ","); quotient = quotient / 10; } strBuilder.deleteCharAt(strBuilder.lastIndexOf(",")); return strBuilder.reverse().toString(); }
/** * 添加单位 * * @param temp * @return */ private String addUnits(String temp) { StringBuffer sb = new StringBuffer(); String str[] = temp.split(","); String tempStr = temp.replace(",", ""); if (tempStr.contains("000")) { return sb.append(resplaceNumToStr(str[0]) + unit[3]).toString(); } else if (tempStr.contains("00")) { if (tempStr.charAt(3) == '0') { return sb.append(resplaceNumToStr(str[0]) + unit[3]).append( resplaceNumToStr(str[1]) + unit[2]).toString(); } else { return sb.append(resplaceNumToStr(str[0]) + unit[3]).append( numStr[0]).append(resplaceNumToStr(str[3])).toString(); } } else { for (int i = 0; i < str.length; i++) { sb.append(resplaceNumToStr(str[i])); if (!str[i].equals("0")) { sb.append(unit[str.length - (i + 1)]); } } } return sb.toString(); }
/** * 数字替换 * * @param str * @return */ private String resplaceNumToStr(String str) { try { int num = Integer.parseInt(str); return numStr[num]; } catch (Exception e) { e.printStackTrace(); } return ""; }
/** * 把4位长度的数字转化成字符串 * * @param number * @param i * @return */ private String transformNumberToString(String number, int i) { StringBuffer strBuffer = new StringBuffer(); StringBuilder strBuilder = new StringBuilder(); try { int num = Integer.parseInt(number); if (num != 0) { String s1 = decomposeNumberToString(num, strBuffer); strBuilder.append(addUnits(s1)); if (i == 1) { strBuilder.append(unit[5]); } else if (i == 2) strBuilder.append(unit[4]); } } catch (Exception e) { e.printStackTrace(); } return strBuilder.toString(); }
/** * 得到最终结果 * * @param str * @return */ public String IntegrationResultString(String str) { StringBuffer strBuffer = new StringBuffer(); String numStr[] = null; if (str.indexOf(".") != -1) { numStr = str.split("\\."); } else { return strBuffer.append(createIntegerPartsResult(str)).toString(); } String fractionalStr = createFractionalPartsResult(numStr[1]); String integerStr = createIntegerPartsResult(numStr[0]); return strBuffer.append(integerStr).append(fractionalStr).toString(); }
private String createIntegerPartsResult(String integer) { StringBuffer strBuffer = new StringBuffer(); String temp = additionalZero(integer); String str1 = temp.substring(0, 4); strBuffer.append(transformNumberToString(str1, 1)); String str2 = temp.substring(4, 8); strBuffer.append(transformNumberToString(str2, 2)); String str3 = temp.substring(8, temp.length()); strBuffer.append(transformNumberToString(str3, 3) + unit2[0]); return strBuffer.toString(); }
private String createFractionalPartsResult(String fractionalStr) { StringBuilder strB = new StringBuilder(); String s1 = fractionalStr.substring(0, 1); String s2 = fractionalStr.substring(1, fractionalStr.length()); if (!s1.equals("0")) { strB.append(resplaceNumToStr(s1) + unit2[1]); } if (!s2.equals("0")) { strB.append(resplaceNumToStr(s2) + unit2[2]); } return strB.toString(); }
public static void main(String args[]) { NumberToString test = new NumberToString(); String str = "200123004054.11"; System.out.println(test.IntegrationResultString(str)); } }
posted on 2010-11-02 14:59 AK47 阅读(600) 评论(0) 编辑 收藏 所属分类: java相关
Powered by: BlogJava Copyright © AK47