日期:2014-05-20 浏览次数:20746 次
import java.text.*; public class ChineseCurrency{ public static void main(String[] args) { double number = 100035.53; System.out.println(toChineseCurrency(number)); } public static String toChineseCurrency(Object o) { if(o instanceof Number) { String s = new DecimalFormat("#.00").format(o); System.out.println(s); s = s.replaceAll("\\.", ""); char[] digit = {'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'}; String unit = "仟佰拾兆仟佰拾亿仟佰拾万仟佰拾元角分"; int l = unit.length(); StringBuilder sb = new StringBuilder(unit); for(int i=s.length()-1; i>=0; i--) sb = sb.insert(l-s.length()+i, digit[(s.charAt(i) - 0x30)]); s = sb.substring(l-s.length(), l+s.length()); s = s.replaceAll("零[拾佰仟]", "零"). replaceAll("零{2,}", "零"). replaceAll("零([兆万元])", "$1"). replaceAll("零[角分]", ""); return s; } else { throw new NumberFormatException(); } } }
------解决方案--------------------
照楼主帖子的意思,也写了完整的一个,供大家参考
import java.util.HashMap;
public class NumberToChinese {
public static void main(String[] args)
{
StringBuffer numberToCn = new StringBuffer();
String number = "-2813040410.0787003560";
HashMap cnHash = getCNHash();
HashMap unitHash = getUnitHash();
String[] partOfNumber = null;
if(number.substring(0, 1).equals("-")) //判断数字是否为负数,并将数划分为小数、整数两部分
{
numberToCn.append("負");
partOfNumber = number.substring(1).split("\\.");
}
else
partOfNumber = number.substring(0).split("\\.");
if(partOfNumber.length == 1) //如果输入的数没有小数则只进行整数部分转化
{
String str = PositiveChangeToCN(partOfNumber[0],cnHash,unitHash);