字符串转换为货币金额大写格式,请大家指教一下
import java.text.DecimalFormat;
import java.util.Scanner;
public class Test080{
private final static String[] STR_NUMBER={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
private final static String[] STR_UNIT={"","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟"};
private final static String[] STR_UNIT2={"厘","分","角"};
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("请输入一个金额:");
String convert=convert(in.nextDouble());
System.out.println(convert);
}
public static String getInteger(String num)
{
if(num.indexOf(".")!=-1)
{
num=num.substring(0,num.indexOf("."));
}
num=new StringBuffer(num).reverse().toString();
StringBuffer temp=new StringBuffer();
for(int i=0;i<num.length();i++)
{
temp.append(STR_UNIT[i]);
temp.append(STR_NUMBER[num.charAt(i)-48]);
}
num=temp.reverse().toString();
num = numReplace(num, "零拾", "零");
num = numReplace(num, "零佰", "零");
num = numReplace(num, "零仟", "零");
num = numReplace(num, "零万", "万");
num = numReplace(num, "零亿", "亿");
num = numReplace(num, "零零", "零");
num = numReplace(num, "亿万", "亿");
if(num.lastIndexOf("零")==num.length()-1)
{
num=num.substring(0,num.length()-1);
}
return num;
}
public static String getDecimal(String num)
{
if(num.indexOf(".")==-1)
{
return "";
}
num=num.substring(num.indexOf(".")+1);
num=new StringBuffer(num).reverse().toString();
StringBuffer temp=new StringBuffer();
for(int i=0;i<num.length();i++)
{
temp.append(STR_UNIT2[i]);
temp.append(STR_NUMBER[num.charAt(i)-48]);
}
num=temp.reverse().toString();
num = numReplace(num, "零角", "零");
num = numReplace(num, "零分", "零");
num = numReplace(num, "零厘", "零");
num = numReplace(num, "零零", "零");
if(num.lastIndexOf("零")==num.length()-1)
{
num=num.substring(0,num.length()-1);
}
return num;
}
public static String numReplace(String num,String oldStr,String newStr)
{
while(true)
{
if(num.indexOf(oldStr)==-1)
{
break;
}
num=num.replaceAll(oldStr, newStr);
}
return num;
}
public static String convert(double d)
{
DecimalFormat df=new DecimalFormat("#0.###");
String strNum=df.format(d);
if(strNum.indexOf(".")!=-1)
{
String num=strNum.substring(0,strNum.indexOf("."));
if(num.length()>12)
{
System.out.println("数字太大,不能完成转换");
return "";
}
}
String point="";
if(strNum.indexOf(".")!=-1)
{
point="元";
}
else
{
point="元整";
}
String result=getInteger(strNum)+point+getDecimal(strNum);
if(result.startsWith("元"))
{
result=result.substring(1,result.length());
}
return result;
}
}
在代码中,temp.append(STR_NUMBER[num.charAt(i)-48]),索引为什么要减去48,不减的话,运行会报错。
...全文
相关推荐
js字符串转货币格式及金额小写转大写
js实现将输入框中的金额转换行财务要求格式 小写金额转大写
输入数字转换为大写金额输出
随意输入一个整数,可以输出对应的大写金额显示。 巧妙使用递归,实现转换功能。
货币大写金额转换插件
javascript货币金额小写立即转换为大写,(ps:网络上下载的)。
java 正则数字金额转换为大写
输入字符,通过正则验证是否为金额不超过万亿精确到小数点后两位,然后将数字转换为大写 包括后台java正则验证及前台js验证 请输入一个数字(精确到小数点后两位): fdsa54325.54 fdsa54325.54 false 请输入一个数字(金额不超过万亿精确到小数点后两位) 请输入一个数字(精确到小数点后两位): 9909010256300.30 9909010256300.30 true 玖万玖仟零玖拾亿壹仟零贰拾伍万陆仟叁佰元叁角整
oracle中将小写金额转换为大写金额函数
oracle中将小写金额转换为大写金额函数 oracle中将小写金额转换为大写金额函数


