写一个方法,将钱后数字金额转换成汉字大写金额

如 2038 转换成汉字金额 二千零三十八元整。
2024-12-15 22:42:33
推荐回答(4个)
回答1:

/**
* 将数字型货币转换为中文型货币

*
*


* String currency="1111111.111"
* String str = StringUtils.getCurrency2CN(currency)
* System.out.println(str); // "壹佰一十一万壹千壹佰一十一元一角一分"
*

*/
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;
}

回答2:

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;
}

回答3:

int money = 2038;

if(money==2038){
System.out.println("二清盯千答行和零三十带知八元整");
}

回答4:

public class no4 {

public static void main(String[] args) {
// 金额大小写转换
float money=2038.0f;
int m1,m2,m3,m4,m5,m6,temp;
temp = (int)(money*100);
System.out.println(money);

m1 =temp%1000000/100000;
m2 =temp%100000/10000;
m3 =temp%10000/游族慧1000;
m4 =temp%1000/100;
m5 =temp%100/10;
m6 =temp%10;

char c1,c2,c3,c4,c5,c6;
c1=transe(m1);
c2=transe(m2);
c3=transe(m3);
c4=transe(m4);
c5=transe(m5);
c6=transe(m6);
String str="";
if(m1!=0)
{
str+=c1+"千";
}
if(m2!=0)
{
str+=c2+"神答百";
}
if(m1!=0&&m2==0&&(m3!=0||m4!=0))
{
str+="零";
}
if(m3!=0)
{
str+=c3+"十";
}
if(m2!=0&&m3==0&&m4!=0)
{
str+="零";
}
if(m4!=0)
{
str+=c4;
}
str+="元";
if(m5 ==0&&m6 ==0)
{
str+="整";
}
if(m5!=0)
{
str+=c5+"角";
}
if(m5==0&&m6!=0)
{
str+="零穗手";
}
if(m6!=0)
{
str+=c6+"分";
}

System.out.print(str+"\n");
System.out.println("..........................");

}

public static char transe(int m)
{
char r=' ';
switch (m)
{
case 0:
r ='零';
break;
case 1:
r ='壹';
break;
case 2:
r ='贰';
break;
case 3:
r ='叁';
break;
case 4:
r ='肆';
break;
case 5:
r ='伍';
break;
case 6:
r ='陆';
break;
case 7:
r ='柒';
break;
case 8:
r ='捌';
break;
case 9:
r ='玖';
break;

}
return r;
}
}