将阿拉伯数字转换成中文汉字格式


import java.text.*;

/**
* @author wang wei wKF22287
* @since 2010
*/
public class CNNumberTools
{

/** "零","壹","贰","叁","肆","伍","陆","柒","捌","玖" */
private final static String[] nums = new String[] { "\u96f6", "\u58f9",
"\u8d30", "\u53c1", "\u8086", "\u4f0d", "\u9646", "\u67d2",
"\u634c", "\u7396" };

/** "拾","佰","仟","万","亿" */
private final static String[] units = new String[] { "\u62fe", "\u4f70",
"\u4edf", "\u4e07", "\u4ebf" };

private final static NumberFormat numberFormat = new DecimalFormat(
"#,####.#");

public static String convertToCNNumber(double number)
{
if (number > Double.MAX_VALUE || number < 0.0)
{
return null;
}

String srcNum = numberFormat.format(number); //按个数转换,如"21,1234,4567,5487.4543544"

String prefixNum = srcNum;
if (srcNum.indexOf(".") != -1)
{
prefixNum = srcNum.substring(0, srcNum.indexOf(".")); //小数点前
}

StringBuffer result = new StringBuffer(0); //用于保存结果

String[] numPices = prefixNum.split(","); //4个数字一组
for (int i = 0; i < numPices.length; i++) //遍历每个组
{
for (int j = 0; j < numPices[i].length(); j++) //遍历组中的每个数字
{
int k = Integer.parseInt(String.valueOf(numPices[i].charAt(j)));
int len = numPices[i].length();
result.append(nums[k]); //变成汉字
result.append(len - 2 - j >= 0 && k > 0 ? units[len - 2 - j]
: ""); //添加仟佰拾
result
.append(j != len - 1 ? ""
: ((i + numPices.length) % 2 == 0 ? (i == numPices.length - 1 ? ""
: units[3])
: i != numPices.length - 1 ? units[4] : "")); //添加亿万
}
}

String resutlStr = result.toString();

resutlStr = resutlStr.replaceAll(nums[0] + "{2,}", nums[0]); //"零零" to "零"
resutlStr = resutlStr.replaceAll(nums[0] + units[3] + "{1}", units[3]); //"零万" to "万"
resutlStr = resutlStr.replaceAll(nums[0] + units[4] + "{1}", units[4]); //"零亿" to "亿"
resutlStr = resutlStr.replaceAll(units[4] + units[3] + "{1}", units[4]); //"亿万" to "亿零"
if (resutlStr.lastIndexOf(nums[0]) == resutlStr.length() - 1)
{ //去掉最后的"零"
resutlStr = resutlStr.substring(0, resutlStr.length() - 1);
}

return resutlStr;
}

public static void main(String[] args)
{
String a = CNNumberTools.convertToCNNumber(Long.MAX_VALUE);
System.out.println(Long.MAX_VALUE);
System.out.println(a);

// 9223372036854775807
// 玖佰贰拾贰亿叁仟叁佰柒拾贰万零叁佰陆拾捌亿伍仟肆佰柒拾柒万陆仟
}

}



一个考试题目,没写全,欢迎指点
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值