此代码根据网上搜集到的部分代码进行修改使得输出准确完整
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()));
}
}