当前位置: 技术问答>java相关
一个没有优化的字符串到中文货币转换bean,欢迎大家测试、优化!
来源: 互联网 发布时间:2015-08-26
本文导语: 不知道对你有没有用?希望大家能提出宝贵意见,谢谢!用法如下: Untitled xuefeng.util.currencyConvertor.java package xuefeng.util; /** * Title: beans * Description: * Copyright: Copyright (c) 2001 * Company:...
不知道对你有没有用?希望大家能提出宝贵意见,谢谢!用法如下:
Untitled
xuefeng.util.currencyConvertor.java
package xuefeng.util;
/**
* Title: beans
* Description:
* Copyright: Copyright (c) 2001
* Company: cchut
* @author xuefeng
* @version 1.0
*/
public class currencyConvertor
{
private final String[] strArray1 = {"零", "壹","贰","叁","肆","伍","陆","柒","捌","玖"};
private final String[][] strArray2 = {{"仟","佰","拾","亿"},{"仟","佰","拾","万"},{"仟","佰","拾","圆"}};
private final String[] strArray3 = {"角","分"};
//Constructor
public currencyConvertor()
{
}
//Delete "," character in str
public String deleteChar(String str)
{
StringBuffer buffer = new StringBuffer(str.length()+1);
for(int i=0;i0);
str = this.deleteChar(str);
String str2 = str.substring(0,str.indexOf(".")); //Integer part of str
String str3 = str.substring(str.indexOf("."),str.length()); //Decimal part of str
int intTmp = str2.length()%4 ;
int k = 0 ; //k: parts of str which divided by 4
if(intTmp==0)
k = str2.length()/4 ;
else
k = (int)(str2.length()/4+1) ;
StringBuffer buffer = new StringBuffer(100); //Stringbuffer contains currency string
for(int i=0;i0)
tmp = str2.substring(str2.length()-(k-i)*4,str2.length()-(k-i-1)*4);
if(Integer.parseInt(tmp)==0)
{
if(i==(k-1))
{
if(buffer.charAt(buffer.length()-1)=='零')
buffer.deleteCharAt(buffer.length()-1);
buffer.append('圆');
continue;
}
else
{
if(buffer.charAt(buffer.length()-1)=='零')
buffer.deleteCharAt(buffer.length()-1);
else
buffer.append(strArray1[0]);
continue;
}
}
for(int j=0;j0&&buffer.charAt(buffer.length()-1)=='零')
continue;
else
buffer.append(strArray1[0]);
}
else
{
intTmp = Integer.parseInt(""+c);
buffer.append(strArray1[intTmp]).append(strArray2[3-k+i][4-tmp.length()+j]);
}
}
else if(j!=0&&j
Untitled
xuefeng.util.currencyConvertor.java
package xuefeng.util;
/**
* Title: beans
* Description:
* Copyright: Copyright (c) 2001
* Company: cchut
* @author xuefeng
* @version 1.0
*/
public class currencyConvertor
{
private final String[] strArray1 = {"零", "壹","贰","叁","肆","伍","陆","柒","捌","玖"};
private final String[][] strArray2 = {{"仟","佰","拾","亿"},{"仟","佰","拾","万"},{"仟","佰","拾","圆"}};
private final String[] strArray3 = {"角","分"};
//Constructor
public currencyConvertor()
{
}
//Delete "," character in str
public String deleteChar(String str)
{
StringBuffer buffer = new StringBuffer(str.length()+1);
for(int i=0;i0);
str = this.deleteChar(str);
String str2 = str.substring(0,str.indexOf(".")); //Integer part of str
String str3 = str.substring(str.indexOf("."),str.length()); //Decimal part of str
int intTmp = str2.length()%4 ;
int k = 0 ; //k: parts of str which divided by 4
if(intTmp==0)
k = str2.length()/4 ;
else
k = (int)(str2.length()/4+1) ;
StringBuffer buffer = new StringBuffer(100); //Stringbuffer contains currency string
for(int i=0;i0)
tmp = str2.substring(str2.length()-(k-i)*4,str2.length()-(k-i-1)*4);
if(Integer.parseInt(tmp)==0)
{
if(i==(k-1))
{
if(buffer.charAt(buffer.length()-1)=='零')
buffer.deleteCharAt(buffer.length()-1);
buffer.append('圆');
continue;
}
else
{
if(buffer.charAt(buffer.length()-1)=='零')
buffer.deleteCharAt(buffer.length()-1);
else
buffer.append(strArray1[0]);
continue;
}
}
for(int j=0;j0&&buffer.charAt(buffer.length()-1)=='零')
continue;
else
buffer.append(strArray1[0]);
}
else
{
intTmp = Integer.parseInt(""+c);
buffer.append(strArray1[intTmp]).append(strArray2[3-k+i][4-tmp.length()+j]);
}
}
else if(j!=0&&j