中文转换大小写工具包(ChineseCapitalNumber)
来源:互联网 发布:电子驱蚊器软件 编辑:程序博客网 时间:2024/06/04 18:38
package com.baiwang.zbxjf.utils;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class ChineseCapitalNumber {
public static String[] chineseDigits = new String[]{
"\u96f6", "\u58f9", "\u8d30", "\u53c1", "\u8086", "\u4f0d", "\u9646", "\u67d2",
"\u634c", "\u7396"};
private static final BigDecimal MAX = new BigDecimal(99999999999999.99d);
private static final BigDecimal MIN = new BigDecimal(-99999999999999.99d);
private static final BigDecimal big100 = new BigDecimal(100);
private static final BigDecimal big10000 = new BigDecimal(10000);
/**
* Convers amount number into Chinese capital number, keeps 2 digits of decimal part by round
*
* @param sAmount
* @return Capital Number in Chinese
*/
public static String amount2Chinese(String sAmount) {
BigDecimal amount = new BigDecimal(sAmount);
if (amount.compareTo(MAX) > 0 || amount.compareTo(MIN) < 0)
throw new IllegalArgumentException(
"parameter overflows (-99999999999999.99 ~ 99999999999999.99)!");
boolean negative = false;
if (amount.compareTo(BigDecimal.ZERO) < 0) {
negative = true;
amount = amount.negate();
}
BigDecimal temp = amount.multiply(big100).setScale(0, RoundingMode.HALF_UP);
int numFen = temp.remainder(BigDecimal.TEN).intValue(); // cent
temp = temp.divideToIntegralValue(BigDecimal.TEN);
int numJiao = temp.remainder(BigDecimal.TEN).intValue(); // dime
temp = temp.divideToIntegralValue(BigDecimal.TEN);
//temp is the integer part
int[] parts = new int[20];
int numParts = 0;
for (int i = 0; ; i++) {
if (temp.compareTo(BigDecimal.ZERO) == 0)
break;
int part = temp.remainder(big10000).intValue();
parts[i] = part;
numParts++;
temp = temp.divideToIntegralValue(big10000);
}
boolean beforeWanIsZero = true; // 10 thousand
String chineseStr = "";
for (int i = 0; i < numParts; i++) {
String partChinese = partTranslate(parts[i]);
if (i % 2 == 0) {
if ("".equals(partChinese))
beforeWanIsZero = true;
else
beforeWanIsZero = false;
}
if (i != 0) {
if (i % 2 == 0)
chineseStr = "\u4ebf" + chineseStr;
else {
if ("".equals(partChinese) && !beforeWanIsZero)
chineseStr = "\u96f6" + chineseStr;
else {
if (parts[i - 1] < 1000 && parts[i - 1] > 0)
chineseStr = "\u96f6" + chineseStr;
chineseStr = "\u4e07" + chineseStr;
}
}
}
chineseStr = partChinese + chineseStr;
}
if ("".equals(chineseStr))
chineseStr = chineseDigits[0];
else if (negative)
chineseStr = "\u8d1f" + chineseStr;
chineseStr = chineseStr + "\u5143";
if (numFen == 0 && numJiao == 0) {
chineseStr = chineseStr + "\u6574";
} else if (numFen == 0) {
chineseStr = chineseStr + chineseDigits[numJiao] + "\u89d2";
} else {
if (numJiao == 0)
chineseStr = chineseStr + "\u96f6" + chineseDigits[numFen] + "\u5206";
else
chineseStr = chineseStr + chineseDigits[numJiao] + "\u89d2"
+ chineseDigits[numFen] + "\u5206";
}
return chineseStr;
}
/**
* Turns a integer which is within range 0~9999 into Chinese capital number,return "" when it is 0
*
* @param amountPart
* @return
*/
private static String partTranslate(int amountPart) {
if (amountPart < 0 || amountPart > 10000) {
throw new IllegalArgumentException("parameter has to be an integer between 0 and 10000");
}
String[] units = new String[]{"", "\u62fe", "\u4f70", "\u4edf"};
int temp = amountPart;
String amountStr = new Integer(amountPart).toString();
int amountStrLength = amountStr.length();
boolean lastIsZero = true;
String chineseStr = "";
for (int i = 0; i < amountStrLength; i++) {
if (temp == 0)
break;
int digit = temp % 10;
if (digit == 0) {
if (!lastIsZero)
chineseStr = "\u96f6" + chineseStr;
lastIsZero = true;
} else {
chineseStr = chineseDigits[digit] + units[i] + chineseStr;
lastIsZero = false;
}
temp = temp / 10;
}
return chineseStr;
}
public static void main(String args[]) throws UnsupportedEncodingException{
System.out.println(amount2Chinese("123134"));
}
}
- 中文转换大小写工具包(ChineseCapitalNumber)
- java 字符大小写(中文)转换
- 中文阿拉伯数据大小写转换(整理)
- 中文阿拉伯数据大小写转换(整理)
- 日期转换工具包(DateUtil)
- 大小写转换(金额转换)
- 大小写转换(金额转换)
- java之获取中文全拼/首字母工具(大小写转换)
- 蓝桥杯 - 大小写转换(水题)
- 无聊的测试(1) 大小写转换(含中文)
- java---金额中文大小写实时转换小程序
- js 数字金额大小写转换成中文大写
- 【大小写转换】
- 大小写转换
- 大小写转换
- 大小写转换
- 大小写转换
- 大小写转换
- android中的动画有哪几类,它们的特点和区别是什么?
- 让火狐等浏览器也能使用HTC(HTML component)的方法
- HDU5806 NanoApe Loves Sequence Ⅱ BestCoder Round #86 two-pointer
- [洛谷luogu]P1108 低价购买
- GB28181国标2016版本协议文档(正式版)解读(四)
- 中文转换大小写工具包(ChineseCapitalNumber)
- 大嘴首发J2EE框架,Bigmouth-framework你值得拥有!
- juqery实现多标签页的切换
- ListView点击事件解析之OnItemSelected、OnItemOnClick
- java编程思想 bruce Eckel chapter6 访问权限控制
- PHP新手易犯错误总结
- SDUT 2772 数据结构实验之串一:KMP简单应用
- 用MVP架构开发Android应用
- html5 ajax文件上传的进度条实现