首页 > 正文

public static String NumToRMBStr(String currencyNo, double val) throws Exception {

String SignStr = \”\”;

String TailStr = \”\”;

long fraction, integer;

int jiao, fen;

if (val < 0) {

val = -val;

SignStr = \”负\”;

}

if (val > 99999999999999.999 || val < -99999999999999.999)

throw new Exception(\”数值位数过大\”); //数值位数过大

// 四舍五入到分

long temp = Math.round(val * 100);

integer = temp / 100;

fraction = temp % 100;

jiao = (int) fraction / 10;

fen = (int) fraction % 10;

if (jiao == 0 && fen == 0) {

TailStr = \”整\”;

} else {

TailStr = HanDigiStr[jiao];

if (jiao != 0)

TailStr += \”角\”;

if (integer == 0 && jiao == 0) // 零元后不写零几分

TailStr = \”\”;

if (fen != 0)

TailStr += HanDigiStr[fen] + \”分\”;

}

return SignStr + PositiveIntegerToHanStr(String.valueOf(integer)) + \”元\” + TailStr;

}

private static String HanDiviStr[] =

new String[] {

\”\”,

\”拾\”,

\”佰\”,

\”仟\”,

\”万\”,

\”拾\”,

\”佰\”,

\”仟\”,

\”亿\”,

\”拾\”,

\”佰\”,

\”仟\”,

\”万\”,

\”拾\”,

\”佰\”,

\”仟\”,

\”亿\”,

\”拾\”,

\”佰\”,

\”仟\”,

\”万\”,

\”拾\”,

\”佰\”,

\”仟\” };

private static String PositiveIntegerToHanStr(String NumStr)

throws Exception { // 输入字符串必须正整数,只允许前导空格(必须右对齐),不宜有前导零

String RMBStr = \”\”;

boolean lastzero = false;

boolean hasvalue = false; // 亿、万进位前有数值标记

int len, n;

len = NumStr.length();

if (len > 15)

throw new Exception(\”数值过大\”); //数值过大

for (int i = len – 1; i >= 0; i–) {

if (NumStr.charAt(len – i – 1) == \’ \’)

continue;

n = NumStr.charAt(len – i – 1) – \’0\’;

if (n < 0 || n > 9)

throw new Exception(\”输入含非数字字符\”); //输入含非数字字符

if (n != 0) {

if (lastzero)

RMBStr += HanDigiStr[0]; // 若干零后若跟非零值,只显示一个零

// 除了亿万前的零不带到后面

//if( !( n==1 && (i%4)==1 && (lastzero || i==len-1) ) ) // 如十进位前有零也不发壹音用此行

if (!(n == 1 && (i % 4) == 1 && i == len – 1)) // 十进位处于第一位不发壹音

RMBStr += HanDigiStr[n];

RMBStr += HanDiviStr[i]; // 非零值后加进位,个位为空

hasvalue = true; // 置万进位前有值标记

} else {

if ((i % 8) == 0 || ((i % 8) == 4 && hasvalue)) { // 亿万之间必须有非零值方显示万

RMBStr += HanDiviStr[i]; // “亿”或“万”

hasvalue = false;

}

}

if ((i % 8) == 0 || (i % 8) == 4)

hasvalue = false;

lastzero = (n == 0) && (i % 4 != 0); // 亿万前有零后不加零,如:拾万贰仟

}

if (RMBStr.length() == 0)

return HanDigiStr[0]; // 输入空字符或\”0\”,返回\”零\”

return RMBStr;

}

/** * 将数字型货币转换为中文型货币<br> * * <pre> * String currency=\”1111111.111\” * String str = StringUtils.getCurrency2CN(currency) * System.out.println(str); // \”壹佰一十一万壹千壹佰一十一元一角一分\” * </pre> */ final static String CARRY_SHI = \”拾\”; final static String CARRY_BAI = \”佰\”; final static String CARRY_QIAN = \”仟\”; final static String CARRY_WAN = \”万\”; final static String CARRY_YI = \”亿\”; final static String CUR_YUAN = \”元\”; final static String CUR_FEN = \”分\”; final static String CUR_JIAO = \”角\”; final static String[] digits = { \”零\”, \”壹\”, \”贰\”, \”叁\”, \”肆\”, \”伍\”, \”陆\”, \”柒\”, \”捌\”, \”玖\”, }; public static String getValueOfCurrency2CN(String priValue) { String value = String.valueOf(priValue); int dot_pos = String.valueOf(value).indexOf(\’.\’); String int_value; String fraction_value; if (dot_pos == -1) { int_value = value; fraction_value = \”00\”; } else { int_value = value.substring(0, dot_pos); fraction_value = value.substring(dot_pos + 1, priValue.length()) + \”00\”.substring(0, 2); } StringBuffer cn_currency = new StringBuffer(); int len = int_value.length(); boolean zero_flag = false; // if (len == 16) // int_value.substring(0, 15); if (len > 12 && len <= 16) { String temp = int_value.substring(0, len – 12); if (Integer.parseInt(temp) != 0) { cn_currency .append(cell2CH(int_value.substring(0, len – 12), zero_flag)) .append(CARRY_WAN); } int_value = int_value.substring(len – 12, len); len = 12; zero_flag = true; } if (len > 8 && len <= 12) { String temp = int_value.substring(0, len – 8); if (Integer.parseInt(temp) != 0) { cn_currency.append(cell2CH(int_value.substring(0, len – 8), zero_flag)) .append(CARRY_YI); } int_value = int_value.substring(len – 8, len); len = 8; } if (len > 4 && len <= 8) { // zero_flag = false; String temp = int_value.substring(0, len – 4); if (Integer.parseInt(temp) != 0) { cn_currency.append(cell2CH(int_value.substring(0, len – 4), zero_flag)) .append(CARRY_WAN); } int_value = int_value.substring(len – 4, len); len = 4; zero_flag = true; } cn_currency.append(cell2CH(int_value.substring(0, len), zero_flag)); if (!cn_currency.toString().equals(\”\”)) { cn_currency.append(CUR_YUAN); } int t = Character.getNumericValue(fraction_value.charAt(0)); if (t != 0) { cn_currency.append(digits[t]).append(CUR_JIAO); } t = Character.getNumericValue(fraction_value.charAt(1)); if (t != 0) { cn_currency.append(digits[t]).append(CUR_FEN); } String returnVaule = cn_currency.toString(); returnVaule = StringUtils.replace(returnVaule, \”零零\”, \”零\”); returnVaule = StringUtils.replace(returnVaule, \”零元\”, \”元\”); returnVaule = StringUtils.replace(returnVaule, \”零万\”, \”万\”); returnVaule = StringUtils.replace(returnVaule, \”零亿\”, \”亿\”); if (!returnVaule.endsWith(CUR_FEN)) { returnVaule = returnVaule + \”整\”; } return returnVaule; }

猜你喜欢
发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论信息