Java 关于中文大写金额与阿拉伯数字 互相转换的问题(eclipse版)

是要能相互转换的,不要只给数字转成中文大写的,最大数字范围达到万亿。
(输入“100002.1”时最好显示“拾万零贰点壹”,而不是“拾万零仟零佰零拾贰点壹”(要是写不出也没关系哦!))。请各位高手指点指点我这个菜鸟,多谢!!写的好的话会加分的!
最新回答
口吻生花

2024-05-02 03:28:34

package test.format;import java.text.NumberFormat;import java.util.HashMap;public class SimpleMoneyFormat {
public static final String EMPTY = "";public static final String ZERO = "零";public static final String ONE = "壹";public static final String TWO = "贰";public static final String THREE = "叁";public static final String FOUR = "肆"; public static final String FIVE = "伍";public static final String SIX = "陆";public static final String SEVEN = "柒"; public static final String EIGHT = "捌"; public static final String NINE = "玖"; public static final String TEN = "拾"; public static final String HUNDRED = "佰"; public static final String THOUSAND = "仟"; public static final String TEN_THOUSAND = "万"; public static final String HUNDRED_MILLION = "亿"; public static final String YUAN = "元"; public static final String JIAO = "角"; public static final String FEN = "分"; public static final String DOT = "."; private static SimpleMoneyFormat formatter = null;private HashMap chineseNumberMap = new HashMap(); private HashMap chineseMoneyPattern = new HashMap(); private NumberFormat numberFormat = NumberFormat.getInstance(); private SimpleMoneyFormat() { numberFormat.setMaximumFractionDigits(4); numberFormat.setMinimumFractionDigits(2); numberFormat.setGroupingUsed(false); chineseNumberMap.put("0", ZERO); chineseNumberMap.put("1", ONE); chineseNumberMap.put("2", TWO); chineseNumberMap.put("3", THREE); chineseNumberMap.put("4", FOUR); chineseNumberMap.put("5", FIVE); chineseNumberMap.put("6", SIX); chineseNumberMap.put("7", SEVEN); chineseNumberMap.put("8", EIGHT); chineseNumberMap.put("9", NINE); chineseNumberMap.put(DOT, DOT); chineseMoneyPattern.put("1", TEN); chineseMoneyPattern.put("2", HUNDRED); chineseMoneyPattern.put("3", THOUSAND); chineseMoneyPattern.put("4", TEN_THOUSAND); chineseMoneyPattern.put("5", TEN); chineseMoneyPattern.put("6", HUNDRED); chineseMoneyPattern.put("7", THOUSAND); chineseMoneyPattern.put("8", HUNDRED_MILLION); }
public static SimpleMoneyFormat getInstance() { if (formatter == null) formatter = new SimpleMoneyFormat(); return formatter; }
public String format(String moneyStr) { checkPrecision(moneyStr); String result; result = convertToChineseNumber(moneyStr); result = addUnitsToChineseMoneyString(result); return result; } public String format(double moneyDouble) { return format(numberFormat.format(moneyDouble)); } public String format(int moneyInt) { return format(numberFormat.format(moneyInt));
} public String format(long moneyLong) { return format(numberFormat.format(moneyLong)); }
public String format(Number moneyNum) { return format(numberFormat.format(moneyNum)); }
private String convertToChineseNumber(String moneyStr) { String result; StringBuffer cMoneyStringBuffer = new StringBuffer(); for (int i = 0; i < moneyStr.length(); i++) { cMoneyStringBuffer.append(chineseNumberMap.get(moneyStr.substring(i, i + 1))); }
//拾佰仟万亿等都是汉字里面才有的单位,加上它们 int indexOfDot = cMoneyStringBuffer.indexOf(DOT); int moneyPatternCursor = 1; for (int i = indexOfDot - 1; i > 0; i--) { cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY + moneyPatternCursor)); moneyPatternCursor = moneyPatternCursor == 8 ? 1 : moneyPatternCursor + 1; } String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf(".")); cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("."), cMoneyStringBuffer.length()); while (cMoneyStringBuffer.indexOf("零拾") != -1) {
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零拾"), cMoneyStringBuffer.indexOf("零拾") + 2, ZERO);
} while (cMoneyStringBuffer.indexOf("零佰") != -1) {
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零佰"), cMoneyStringBuffer.indexOf("零佰") + 2, ZERO);
}while (cMoneyStringBuffer.indexOf("零仟") != -1) {
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零仟"), cMoneyStringBuffer.indexOf("零仟") + 2, ZERO);
}while (cMoneyStringBuffer.indexOf("零万") != -1) {
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零万"), cMoneyStringBuffer.indexOf("零万") + 2, TEN_THOUSAND);
} while (cMoneyStringBuffer.indexOf("零亿") != -1) {
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零亿"), cMoneyStringBuffer.indexOf("零亿") + 2, HUNDRED_MILLION); } while (cMoneyStringBuffer.indexOf("零零") != -1) {
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零零"), cMoneyStringBuffer.indexOf("零零") + 2, ZERO);
} if (cMoneyStringBuffer.lastIndexOf(ZERO) == cMoneyStringBuffer.length() - 1) cMoneyStringBuffer.delete(cMoneyStringBuffer.length() - 1, cMoneyStringBuffer.length()); cMoneyStringBuffer.append(fractionPart);
result = cMoneyStringBuffer.toString(); return result; }private String addUnitsToChineseMoneyString(String moneyStr) { String result; StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr); int indexOfDot = cMoneyStringBuffer.indexOf(DOT); cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1, YUAN); cMoneyStringBuffer.insert(cMoneyStringBuffer.length() - 1, JIAO); cMoneyStringBuffer.insert(cMoneyStringBuffer.length(), FEN); if (cMoneyStringBuffer.indexOf("零角零分") != -1)//没有零头,加整
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零角零分"), cMoneyStringBuffer.length(), "整"); else if (cMoneyStringBuffer.indexOf("零分") != -1)//没有零分,加整 cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零分"), cMoneyStringBuffer.length(), "整");
else { if(cMoneyStringBuffer.indexOf("零角")!=-1)
cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("零角"),cMoneyStringBuffer.indexOf("零角")+2);
tmpBuffer.append("整"); } result = cMoneyStringBuffer.toString(); return result; } private void checkPrecision(String moneyStr) {
int fractionDigits = moneyStr.length() - moneyStr.indexOf(DOT) - 1; if (fractionDigits > 2) throw new RuntimeException("金额" + moneyStr + "的小数位多于两位。"); //精度不能比分低 }public static void main(String args[]) { System.out.println(getInstance().format(new Double(10010001.01))); }}
浮生远离

2024-05-02 01:34:04

eclipse中用java实现中文和阿拉伯数字互转的方法如下:
import java.io.*;
import java.lang.IllegalArgumentException;
public class ConvertNum{
/**
* 把金额阿拉伯数字转换为汉字表示,小数点后四舍五入保留两位
* 还有一种方法可以在转换的过程中不考虑连续0的情况,然后对最终的结果进行一次遍历合并连续的零
*/
public static String [] ChineseNum = new String[]{"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
public static String NumToChinese(double num){
if(num > 99999999999999.99 || num < -99999999999999.99)
throw new IllegalArgumentException("参数值超出允许范围 (-99999999999999.99 ~ 99999999999999.99)!");
boolean negative = false;//正负标号
if(num<0){
negative = true;
num = num*(-1);
}
long temp = Math.round(num*100);
int numFen=(int)(temp%10);//分
temp=temp/10;
int numJiao = (int)(temp%10);//角
temp=temp/10;
//此时temp只包含整数部分
int [] parts =new int[20];//将金额整数部分分为在0-9999之间数的各个部分
int numParts = 0;//记录把原来金额整数部分分割为几个部分
for(int i=0;;i++){
if(temp == 0)
break;
int part = (int)(temp%10000);
parts[i] =part;
temp = temp/10000;
numParts++;
}
boolean beforeWanIsZero = true;//标志位,记录万的下一级是否为0
String chineseStr = "";
for(int i=0;i<numParts;i++){
String partChinese = partConvert(parts[i]);
if(i%2==0){
if("".equals(partChinese))
beforeWanIsZero = true;
else
beforeWanIsZero = false;
}
if(i!=0){
if(i%2==0)//亿的部分
chineseStr = "亿"+chineseStr;
else{
if("".equals(partChinese)&&!beforeWanIsZero)// 如果“万”对应的 part 为 0,而“万”下面一级不为 0,则不加“万”,而加“零”
chineseStr = "零"+chineseStr;
else{
if(parts[i-1]<1000&&parts[i-1]>0)//如果万的部分不为0,而万前面的部分小于1000大于0,则万后面应该跟零
chineseStr = "零"+chineseStr;
chineseStr = "万"+chineseStr;
}
}
}
chineseStr = partChinese + chineseStr;
}
if("".equals(chineseStr))//整数部分为0,则表示为零元
chineseStr = ChineseNum[0];
else if(negative)//整数部分部位0,但是为负数
chineseStr = "负" +chineseStr;
chineseStr = chineseStr + "元";
if(numFen==0&&numJiao==0){
chineseStr = chineseStr +"整";
}
else if(numFen==0){//0分
chineseStr = chineseStr +ChineseNum[numJiao] + "角";
}
else{
if(numJiao==0)
chineseStr = chineseStr + "零" + ChineseNum[numFen] + "分";
else
chineseStr = chineseStr + ChineseNum[numJiao] + "角" + ChineseNum[numFen] + "分";
}
return chineseStr;
}
//转换拆分后的每个部分,0-9999之间
public static String partConvert(int partNum){
if(partNum<0||partNum>10000){
throw new IllegalArgumentException("参数必须是大于等于0或小于10000的整数");
}
String [] units = new String[]{"","拾","佰","仟"};
int temp = partNum;
String partResult = new Integer(partNum).toString();
int partResultLength = partResult.length();
boolean lastIsZero = true;//记录上一位是否为0
String chineseStr = "";
for(int i=0;i<partResultLength;i++){
if(temp == 0)//高位无数字
break;
int digit = temp%10;
if(digit == 0){
if(!lastIsZero)//如果前一个数字不是0则在当前汉字串前加零
chineseStr = "零"+chineseStr;
lastIsZero = true;
}
else{
chineseStr = ChineseNum[digit] + units[i] +chineseStr;
lastIsZero = false;
}
temp =temp/10;
}
return chineseStr;
}
public static void main(String args []){
double num = 0;
System.out.println("请输入金额数字,-1退出");
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
num = Double.parseDouble(br.readLine());
}catch(IOException e){
System.out.println(e.toString());
}
while(num!=-1){
System.out.println(num+NumToChinese(num));
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
num = Double.parseDouble(br.readLine());
}catch(IOException e){
System.out.println(e.toString());
}
}
System.out.println("其他测试:");
System.out.println("100120: " + NumToChinese(100120));
System.out.println("25000000000005.999: " + NumToChinese(25000000000005.999));
System.out.println("45689263.626: " + NumToChinese(45689263.626));
System.out.println("0.69457: " + NumToChinese(0.69457));
System.out.println("253.0: " + NumToChinese(253.0));
System.out.println("0: " + NumToChinese(0));
}
}
丶小嘴灬乱亲

2024-05-02 00:08:17

最简单的...
public class T {
public static void main(String[] args) {
zhuanhuan(10001101);
}
private static void zhuanhuan(int s) {
String[] c = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
String[] d = { "元", "拾", "百", "仟", "万","拾万","百万","千万","亿" };
String ss = s + "";//数字强行转换成字符串
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < ss.length(); i++) {
String k = ss.substring(i, i + 1);
int f = Integer.parseInt(k);
if (f == 0) {
int u = Integer.parseInt(ss.substring(i - 1, i));
if (u != 0) {
buffer.append(c[0]);
}
} else {
buffer.append(c[f]);
buffer.append(d[ss.length() - i - 1]);
}
}
System.out.println(buffer.toString());
}
}
バ快乐De右岸ヤ

2024-05-02 09:55:26

这个得自己写函数了。呵呵。我有个VB的,晚上可以发给你看看。