import org.apache.commons.lang3.StringUtils;
public class MonetaryConversionUtils{
private static final String[] CN_UPPER_NUMBER = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
private static final String[] CN_UPPER_MONETRAY_UNIT = {"分","角","元","拾","佰","仟","万","亿"};
private static final long YI = 100000000L;
private static final long WAN = 10000L;
/**
* 把阿拉伯数字金额转换为中文大写金额
* @param number
* @return
*/
public static String convertNumberToCNNumber(double number){
if(number==0){
return CN_UPPER_NUMBER[0]+CN_UPPER_MONETRAY_UNIT[2];
}else{
String result = convertNumberToCNNumberBeforeDot(number)
+ CN_UPPER_MONETRAY_UNIT[2] + convertNumberToCNNumberAfterDot(number);
if(result.length()>2&&result.startsWith(CN_UPPER_NUMBER[0])){
result = result.substring(1);
}
return result;
}
}
/**
* 把阿拉伯数字金额转换为中文大写金额:转换小数点之前的金额
* @param number
* @return
*/
protected static String convertNumberToCNNumberBeforeDot(double number){
StringBuffer strBuf = new StringBuffer();
double nYi = Math.floor(number/YI);
double mYi = Math.floor(number%YI);
if(nYi>0){
return strBuf.append(convertNumberToCNNumberBeforeDot(nYi))
.append(CN_UPPER_MONETRAY_UNIT[7])
.append(convertNumberToCNNumberBeforeDot(mYi))
.toString();
}else{
double nWan = Math.floor(number/WAN);
double mWan = Math.floor(number%WAN);
if(nWan>0){
return strBuf.append(convertNumberToCNNumberBeforeDot(nWan))
.append(CN_UPPER_MONETRAY_UNIT[6])
.append(convertNumberToCNNumberBeforeDot(mWan))
.toString();
}else{
double num = Math.floor(number%WAN);
String strNum = String.valueOf(num);
if(strNum.contains(".")){
strNum = strNum.substring(0, strNum.indexOf("."));
}
if(strNum.length()<4){
String preStr = "";
for(int i=strNum.length(); i<4; i++){
preStr += "0";
}
strNum = preStr + strNum;
}
int index = 2;
for(int i=strNum.length()-1; i>=0; i--){
int n = Integer.parseInt(strNum.charAt(i)+"");
String cnNumber = CN_UPPER_NUMBER[n];
String cnUnit = CN_UPPER_MONETRAY_UNIT[index++];
if(n == 0){
if(strBuf.length()>0&&!CN_UPPER_NUMBER[0].equals(strBuf.charAt(0)+"")){
strBuf.insert(0, cnNumber);
}
}else{
if(i == strNum.length()-1){
strBuf.insert(0, cnNumber);
}else{
strBuf.insert(0, cnNumber+cnUnit);
}
}
}
return strBuf.toString();
}
}
}
/**
* 把阿拉伯数字金额转换为中文大写金额:转换小数点之后的金额
* @param number
* @return
*/
protected static String convertNumberToCNNumberAfterDot(double number){
String result = "";
String strNum = String.valueOf(number);
if(strNum.contains(".")){
strNum = strNum.substring(strNum.indexOf(".")+1);
if(StringUtils.isNotBlank(strNum)){
if(strNum.length()>=2){
Integer i = Integer.parseInt(strNum.charAt(0)+"");
Integer j = Integer.parseInt(strNum.charAt(1)+"");
if(i==0){
if(j!=0){
result = CN_UPPER_NUMBER[i] + CN_UPPER_NUMBER[j] + CN_UPPER_MONETRAY_UNIT[0];
}
}else{
if(j!=0){
result = CN_UPPER_NUMBER[i] + CN_UPPER_MONETRAY_UNIT[1]
+ CN_UPPER_NUMBER[j] + CN_UPPER_MONETRAY_UNIT[0];
}else{
result = CN_UPPER_NUMBER[i] + CN_UPPER_MONETRAY_UNIT[1];
}
}
}else if(strNum.length()>=1){
Integer i = Integer.parseInt(strNum.charAt(0)+"");
if(i!=0){
result = CN_UPPER_NUMBER[i] + CN_UPPER_MONETRAY_UNIT[1];
}
}
}
}
return result;
}
}
阿拉伯数字金额转中文数字金额代码
最新推荐文章于 2024-03-21 23:52:33 发布