java

位置:IT落伍者 >> java >> 浏览文章

Java转换大写金额的代码


发布日期:2019年06月07日
 
Java转换大写金额的代码

此代码根据网上搜集到的部分代码进行修改使得输出准确完整

package PrintOperate;

import javatextNumberFormat;

import javautilHashMap;

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 = NumberFormatgetInstance();

private SimpleMoneyFormat()

{

numberFormatsetMaximumFractionDigits();

numberFormatsetMinimumFractionDigits();

numberFormatsetGroupingUsed(false);

chineseNumberMapput( ZERO);

chineseNumberMapput( ONE);

chineseNumberMapput( TWO);

chineseNumberMapput( THREE);

chineseNumberMapput( FOUR);

chineseNumberMapput( FIVE);

chineseNumberMapput( SIX);

chineseNumberMapput( SEVEN);

chineseNumberMapput( EIGHT);

chineseNumberMapput( NINE);

chineseNumberMapput(DOT DOT);

chineseMoneyPatternput( TEN);

chineseMoneyPatternput( HUNDRED);

chineseMoneyPatternput( THOUSAND);

chineseMoneyPatternput( TEN_THOUSAND);

chineseMoneyPatternput( TEN);

chineseMoneyPatternput( HUNDRED);

chineseMoneyPatternput( THOUSAND);

chineseMoneyPatternput( 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(numberFormatformat(moneyDouble));

}

public String format(int moneyInt)

{

return format(numberFormatformat(moneyInt));

}

public String format(long moneyLong)

{

return format(numberFormatformat(moneyLong));

}

public String format(Number moneyNum)

{

return format(numberFormatformat(moneyNum));

}

private String convertToChineseNumber(String moneyStr)

{

String result;

StringBuffer cMoneyStringBuffer = new StringBuffer();

for (int i = ; i < moneyStrlength(); i++)

{

cMoneyStringBufferappend(chineseNumberMapget(moneyStrsubstring(i i + )));

}

//拾佰仟万亿等都是汉字里面才有的单位加上它们

int indexOfDot = cMoneyStringBufferindexOf(DOT);

int moneyPatternCursor = ;

for (int i = indexOfDot ; i > ; i)

{

cMoneyStringBufferinsert(i chineseMoneyPatternget(EMPTY + moneyPatternCursor));

moneyPatternCursor = moneyPatternCursor == ? : moneyPatternCursor + ;

}

String fractionPart = cMoneyStringBuffersubstring(cMoneyStringBufferindexOf());

cMoneyStringBufferdelete(cMoneyStringBufferindexOf() cMoneyStringBufferlength());

while (cMoneyStringBufferindexOf(零拾) != )

{

cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零拾) cMoneyStringBufferindexOf(零拾) + ZERO);

}

while (cMoneyStringBufferindexOf(零佰) != )

{

cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零佰) cMoneyStringBufferindexOf(零佰) + ZERO);

}

while (cMoneyStringBufferindexOf(零仟) != )

{

cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零仟) cMoneyStringBufferindexOf(零仟) + ZERO);

}

while (cMoneyStringBufferindexOf(零万) != )

{

cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零万) cMoneyStringBufferindexOf(零万) + TEN_THOUSAND);

}

while (cMoneyStringBufferindexOf(零亿) != )

{

cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零亿) cMoneyStringBufferindexOf(零亿) + HUNDRED_MILLION);

}

while (cMoneyStringBufferindexOf(零零) != )

{

cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零零) cMoneyStringBufferindexOf(零零) + ZERO);

}

if (cMoneyStringBufferlastIndexOf(ZERO) == cMoneyStringBufferlength() )

{

cMoneyStringBufferdelete(cMoneyStringBufferlength() cMoneyStringBufferlength());

}

cMoneyStringBufferappend(fractionPart);

result = cMoneyStringBuffertoString();

return result;

}

private String addUnitsToChineseMoneyString(String moneyStr)

{

String result;

StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr);

int indexOfDot = cMoneyStringBufferindexOf(DOT);

cMoneyStringBufferreplace(indexOfDot indexOfDot + YUAN);

cMoneyStringBufferinsert(cMoneyStringBufferlength() JIAO);

cMoneyStringBufferinsert(cMoneyStringBufferlength() FEN);

if (cMoneyStringBufferindexOf(零角零分) != )//没有零头加整

{

cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零角零分) cMoneyStringBufferlength() );

}

else if (cMoneyStringBufferindexOf(零分) != )//没有零分不输出零分也不加整

{

cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零分) cMoneyStringBufferlength() );

}

else if (cMoneyStringBufferindexOf(零角) != )

{

if(cMoneyStringBufferindexOf(零角)==(cMoneyStringBufferlength()))

{

cMoneyStringBufferreplace(cMoneyStringBufferindexOf(零角) cMoneyStringBufferindexOf(零角) + ZERO);

}

else

{

cMoneyStringBufferdelete(cMoneyStringBufferindexOf(零角) cMoneyStringBufferindexOf(零角) + );

}

}

result = cMoneyStringBuffertoString();

return result;

}

private void checkPrecision(String moneyStr)

{

int fractionDigits = moneyStrlength() moneyStrindexOf(DOT) ;

if (fractionDigits > )

{

throw new RuntimeException(金额 + moneyStr + 的小数位多于两位); //精度不能比分低

}

}

public static void main(String args[])

{

Systemoutprintln(getInstance()format(new Double()));

}

}

               

上一篇:JAVA 物理路径上传附件功能

下一篇:Java桌面端程序开发