package zy.cloud.wms.common.utils;
|
|
/**
|
* Created by vincent on 2021/9/11
|
*/
|
public class NumToCN {
|
|
private static final String [] pattern ={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
|
private static final String [] cPattern ={"","拾","佰","仟","万","拾","佰","仟","亿"};
|
private static final String [] cfPattern = {"","角","分"};
|
private static final String ZERO = "零";
|
|
public static String format(String moneyString){
|
int dotPoint = moneyString.indexOf("."); //判断是否为小数
|
String moneyStr;
|
if(dotPoint != -1){
|
moneyStr = moneyString.substring(0,moneyString.indexOf("."));
|
}
|
else{
|
moneyStr = moneyString;
|
}
|
StringBuffer fraction; //小数部分的处理,以及最后的yuan.
|
StringBuilder ms = new StringBuilder();
|
for(int i = 0;i < moneyStr.length();i++){
|
ms.append(pattern[moneyStr.charAt(i) - 48]); //按数组的编号加入对应大写汉字
|
}
|
int cpCursor = 1;
|
for(int j = moneyStr.length() - 1;j > 0;j--){
|
ms.insert(j,cPattern[cpCursor]); //在j之后加字符,不影响j对原字符串的相对位置
|
cpCursor = cpCursor == 8?1:cpCursor + 1; //亿位之后重新循环
|
}
|
while(ms.indexOf("零拾") != -1){ //当十位为零时用一个"零"代替"零拾"
|
//replace的起始于终止位置
|
ms.replace(ms.indexOf("零拾"),ms.indexOf("零拾") + 2,ZERO);
|
}
|
while(ms.indexOf("零佰") != -1){ //当百位为零时,同理
|
ms.replace(ms.indexOf("零佰"),ms.indexOf("零佰") + 2,ZERO);
|
}
|
while(ms.indexOf("零仟") != -1){ //同理
|
ms.replace(ms.indexOf("零仟"),ms.indexOf("零仟") + 2,ZERO);
|
}
|
while(ms.indexOf("零万") != -1){ //万需保留,中文习惯
|
ms.replace(ms.indexOf("零万"),ms.indexOf("零万") + 2,"万");
|
}
|
while(ms.indexOf("零亿") != -1){ //同上
|
ms.replace(ms.indexOf("零亿"),ms.indexOf("零亿") + 2,"亿");
|
}
|
while(ms.indexOf("零零") != -1){//有连续数位出现零,即有以下情况,此时根据习惯保留一个零即可
|
ms.replace(ms.indexOf("零零"),ms.indexOf("零零") + 2,ZERO);
|
}
|
while(ms.indexOf("亿万") != -1){ //特殊情况,如:100000000,根据习惯保留高位
|
ms.replace(ms.indexOf("亿万"),ms.indexOf("亿万") + 2,"亿");
|
}
|
while(ms.lastIndexOf("零") == ms.length()-1){ //当结尾为零j,不必显示,经过处理也只可能出现一个零
|
if(ms.indexOf("零") == -1){
|
ms.delete(ms.lastIndexOf("零"),ms.lastIndexOf("零") + 1);
|
}else{
|
break;
|
}
|
}
|
int end;
|
if((dotPoint = moneyString.indexOf(".")) != -1 ){ //是小数的进入
|
String fs = moneyString.substring(dotPoint + 1,moneyString.length());
|
if(!fs.contains("00") || fs.indexOf("00") >= 2){//若前两位小数全为零,则跳过操作
|
end = Math.min(fs.length(), 2); //仅保留两位小数
|
fraction = new StringBuffer(fs.substring(0,end));
|
for(int j = 0;j < fraction.length();j++){
|
fraction.replace(j,j+1, pattern[fraction.charAt(j) - 48]); //替换大写汉字
|
}
|
for(int i = fraction.length();i > 0;i--){ //插入中文标识
|
fraction.insert(i,cfPattern[i]);
|
}
|
fraction.insert(0,"元"); //为整数部分添加标识
|
}
|
else{
|
fraction = new StringBuffer("元整");
|
}
|
}
|
else{
|
fraction = new StringBuffer("元整");
|
}
|
//加入小数部分
|
ms.append(fraction);
|
return ms.toString();
|
}
|
|
public static void main(String [] ar){
|
System.out.println(NumToCN.format("10005022.123009"));
|
System.out.println(NumToCN.format("0.12"));
|
}
|
|
}
|