数字转中文人民币符号和日期转中文大写
package com.nxt.jycz.core; import java.math.BigDecimal; import java.text.NumberFormat; import java.util.Date;
/**
* Created by IntelliJ IDEA.
* User: zengch
* Date: 2010-12-21
* Time: 11:22:16
* To change this template use File | Settings | File Templates.
*/
public class NumberChUtil {
/**
* step 1
* 小写计量数字传唤为中文大写
*
* @param num 小写: 202.1
* @return 大写:贰佰零贰点壹
*/
public static String numToChinese(String num) {
int i;
for (i = num.length() - 1; i >= 0; i--) {
num = num.replace(",", "");//替换tomoney()中的“,”
num = num.replace(" ", "");//替换tomoney()中的空格
}
System.out.println(num);
try {
Double.parseDouble(num);
} catch (Exception ex) {
log.debug("请检查小写是否正常~");
return "";
}
try {
return toChineseCharacter(Double.parseDouble(num));
} catch (Exception ex) {
log.error(ex.getMessage());
}
return "";
}
/**
* 将数字转为汉字大写 step 2
*
* @param num
* @return
* @throws Exception
*/
public static String toChineseCharacter(double num) throws Exception {
double temp = 0;
long l = Math.abs((long) num);
BigDecimal bil = new BigDecimal(l);
if (bil.toString().length() > 14) {
throw new Exception("数字太大,计算精度不够!");
}
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
int i = 0;
String result = "", sign = "", tempStr = "", temp1 = "";
String[] arr = null;
sign = num < 0 ? "负" : "";
temp = Math.abs(num);
if (l == temp) {
result = doForEach(new BigDecimal(temp).multiply(new
BigDecimal(100)).toString(),
sign);
} else {
nf.setMaximumFractionDigits(2);
temp1 = nf.format(temp);
arr = temp1.split(",");
while (i < arr.length) {
tempStr += arr[i];
i++;
}
BigDecimal b = new BigDecimal(tempStr);
b = b.multiply(new BigDecimal(100));
tempStr = b.toString();
if (tempStr.indexOf(".") == tempStr.length() - 3) {
result = doForEach(tempStr.substring(0,
tempStr.length() - 3), sign);
} else {
result = doForEach(tempStr.substring(0,
tempStr.length() - 3) + "0", sign);
}
}
return result;
}
/**
* 将数字转为汉字大写 step 3 循环替换
*
* @param result
* @param sign
* @return
*/
public static String doForEach(String result, String sign) {
// System.out.println("打印最后处理数:" + result);
String flag = "", b_string = ""; // String[] arr = { "分", "角", "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", // "拾", "佰", "仟", "万", "拾" };
String[] arr = {"", "", "点", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿",
"拾", "佰", "仟", "万", "拾"};
String[] arr1 = {"壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
boolean zero = true;
int len = 0, i = 0, z_count = 0;
if (result == null) {
len = 0;
} else {
len = result.length();
}
while (i < len) {
flag = result.substring(i, i + 1);
i++;
// System.out.println("\n"+flag);
if (flag.equals("0")) {
if (len - i == 10 || len - i == 6 || len - i == 2) {
if (zero) {
b_string = b_string.substring(0,
(b_string.length()) - 1);
zero = false;
}
if (len - i == 10) {
b_string = b_string + "亿";
}
if (len - i == 6) {
b_string = b_string + "万";
}
if (len - i == 2) {
b_string = b_string + "点";
}
// if (len == i) {
// b_string = b_string + "点";
// }
// z_count = 0;
}
else {
if (z_count == 0&&len-i>1) {
b_string = b_string + "零";
zero = true;
}
z_count = z_count + 1;
}
} else {
b_string = b_string + arr1[Integer.parseInt(flag) - 1]
+ arr[len - i];
z_count = 0;
zero = false;
}
// System.out.println(b_string);
}
b_string = sign + b_string;
if ("点".equals(b_string.substring(b_string.length()-1,b_string.length()))){
b_string = b_string.substring(0, b_string.length() - 1);
}
return b_string;
}
/**
* 字符串日期转换成中文格式日期
*
* @param date 字符串日期 yyyy-MM-dd
* @return yyyy年MM月dd日
* @throws Exception
*/
public static String strDateToCnDate(String date) {
String result = "";
String[] cnDate = new String[]{"Ο", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
String ten = "十";
String[] dateStr = date.split("-");
for (int i = 0; i < dateStr.length; i++) {
for (int j = 0; j < dateStr[i].length(); j++) {
String charStr = dateStr[i];
String str = String.valueOf(charStr.charAt(j));
if (charStr.length() == 2) {
if (charStr.equals("10")) {
result += ten;
break;
} else {
if (j == 0) {
if (charStr.charAt(j) == '1')
result += ten;
else if (charStr.charAt(j) == '0')
result += "";
else
result += cnDate[Integer.parseInt(str)] + ten;
}
if (j == 1) {
if (charStr.charAt(j) == '0')
result += "";
else
result += cnDate[Integer.parseInt(str)];
}
}
} else {
result += cnDate[Integer.parseInt(str)];
}
}
if (i == 0) {
result += "年";
continue;
}
if (i == 1) {
result += "月";
continue;
}
if (i == 2) {
result += "日";
continue;
}
}
return result;
}
/**
* 日期转换成中文格式日期
*
* @param _date 日期类型 yyyy-MM-dd
* @return yyyy年MM月dd日
* @throws Exception
*/
public static String dateToCnDate(Date _date) {
String date = DateTools.shortDate(_date);
String result = "";
String[] cnDate = new String[]{"Ο", "一", "二", "三", "四", "五", "六", "七", "
"}; 八", "九
String ten = "十";
String[] dateStr = date.split("-");
for (int i = 0; i < dateStr.length; i++) {
for (int j = 0; j < dateStr[i].length(); j++) {
String charStr = dateStr[i];
String str = String.valueOf(charStr.charAt(j));
if (charStr.length() == 2) {
if (charStr.equals("10")) {
result += ten;
break;
} else {
if (j == 0) {
if (charStr.charAt(j) == '1')
result += ten;
else if (charStr.charAt(j) == '0')
result += "";
else
result += cnDate[Integer.parseInt(str)] + ten;
}
if (j == 1) {
if (charStr.charAt(j) == '0')
result += "";
else
result += cnDate[Integer.parseInt(str)];
}
}
} else {
result += cnDate[Integer.parseInt(str)];
}
}
if (i == 0) {
result += "年";
continue;
}
if (i == 1) {
result += "月";
continue;
}
if (i == 2) {
result += "日";
continue;
}
}
return result;
}
/**
* 字符串数字转换成中文格式数字
*
* @param num 字符串数字 123456
* @return 一二三四五六
* @throws Exception
*/
public static String numToCnNum(String num) {
String result = "";
String[] cnDate = new String[]{"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九
"};
String ten = "十";
String[] dateStr = num.split("-");
for (int i = 0; i < dateStr.length; i++) {
for (int j = 0; j < dateStr[i].length(); j++) {
String charStr = dateStr[i];
String str = String.valueOf(charStr.charAt(j));
if (charStr.length() == 2) {
if (charStr.equals("10")) {
result += ten;
break;
} else {
if (j == 0) {
if (charStr.charAt(j) == '1')
result += ten;
else if (charStr.charAt(j) == '0')
result += "";
else
result += cnDate[Integer.parseInt(str)] + ten;
}
if (j == 1) {
if (charStr.charAt(j) == '0')
result += "";
else
result += cnDate[Integer.parseInt(str)];
}
}
} else {
result += cnDate[Integer.parseInt(str)];
}
}
}
return result;
}
public static void main(String[] args) {
String num="1106.0";
System.out.println(numToChinese(num));
// if (args.length == 0) {
// System.out.println("转换演示:");
// System.out.println("-------------------------"); // System.out.println("202.1: " + numToChinese("202.1")); // System.out.println("2010-10-1: " + strDateToCnDate("2010-10-1"));
// System.out.println("12345: " + numToCnNum("12345")); // } else {
// System.out.println("转换结果:");
// System.out.println(args[0] + ": " + numToChinese(args[0])); // }
}
}