怎么把一个字符串转换为汉字
2015-01-27 16:31
288 查看
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.oa.pub;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author Administrator
*/
public class Amount2RMB {
private static final Pattern AMOUNT_PATTERN = Pattern.compile("^(-?)(0|[1-9]\\d{0,11})\\.(\\d{0,4})"); // 不考虑分隔符的正确性
private static final char[] RMB_NUMS = "零壹贰叁肆伍陆柒捌玖".toCharArray();
private static final String[] UNITS = {"元", "角", "分", "整","厘","毫"};
private static final String[] U1 = {"", "拾", "佰", "仟"};
private static final String[] U2 = {"", "万", "亿"};
/**
* 将金额(整数部分等于或少于12位)转换为中文大写形式.
* @param amount 金额数字
* @return 中文大写
* @throws IllegalArgumentException
*/
public static String convert(String amount) throws IllegalArgumentException {
// 去掉分隔符
amount = amount.replace(",", "");
double num=Double.parseDouble(amount);
amount=String.format("%.2f",num);
// 验证金额正确性
if (amount.equals("0.00")) {
throw new IllegalArgumentException("金额不能为零.");
}
Matcher matcher = AMOUNT_PATTERN.matcher(amount);
if (!matcher.find()) {
throw new IllegalArgumentException("输入金额有误.");
}
String trigger=matcher.group(1);
String integer = matcher.group(2); // 整数部分
String fraction = matcher.group(3); // 小数部分
String result = "";
if(trigger.equals("-")){
result += "负";
}
if (!integer.equals("0")) {
result += integer2rmb(integer) + UNITS[0]; // 整数部分
}
if (fraction.matches("0*")) {
result += UNITS[3]; // 添加[整]
} else if (fraction.startsWith("0") && integer.equals("0")) {
result += fraction2rmb(fraction).substring(1); // 去掉分前面的[零]
} else {
result += fraction2rmb(fraction); // 小数部分
}
return result;
}
// 将金额小数部分转换为中文大写
private static String fraction2rmb(String fraction) {
StringBuffer sb=new StringBuffer();
char jiao='0';
char fen='0';
char li='0';
char hao='0';
int length=fraction.length();
if(length>0){
jiao = fraction.charAt(0); // 角
}
if(length>1){
fen = fraction.charAt(1); // 分
}
if(length>2){
li=fraction.charAt(2);
}
if(length>3){
hao=fraction.charAt(3);
}
return (RMB_NUMS[jiao - '0'] + (jiao > '0' ? UNITS[1] : ""))
+ (fen > '0' ? RMB_NUMS[fen - '0'] + UNITS[2] : "")
+ (li>'0'?RMB_NUMS[li-'0']+UNITS[4]:"")
+ (hao>'0'?RMB_NUMS[hao-'0']+UNITS[5]:"");
}
// 将金额整数部分转换为中文大写
private static String integer2rmb(String integer) {
StringBuilder buffer = new StringBuilder();
// 从个位数开始转换
int i, j;
for (i = integer.length() - 1, j = 0; i >= 0; i--, j++) {
char n = integer.charAt(i);
if (n == '0') {
// 当n是0且n的右边一位不是0时,插入[零]
if (i < integer.length() - 1 && integer.charAt(i + 1) != '0') {
buffer.append(RMB_NUMS[0]);
}
// 插入[万]或者[亿]
if (j % 4 == 0) {
if (i > 0 && integer.charAt(i - 1) != '0'
|| i > 1 && integer.charAt(i - 2) != '0'
|| i > 2 && integer.charAt(i - 3) != '0') {
buffer.append(U2[j / 4]);
}
}
} else {
if (j % 4 == 0) {
buffer.append(U2[j / 4]); // 插入[万]或者[亿]
}
buffer.append(U1[j % 4]); // 插入[拾]、[佰]或[仟]
buffer.append(RMB_NUMS[n - '0']); // 插入数字
}
}
return buffer.reverse().toString();
}
public static void main(String[] args){
System.out.println(Amount2RMB.convert("-20.09"));
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.oa.pub;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author Administrator
*/
public class Amount2RMB {
private static final Pattern AMOUNT_PATTERN = Pattern.compile("^(-?)(0|[1-9]\\d{0,11})\\.(\\d{0,4})"); // 不考虑分隔符的正确性
private static final char[] RMB_NUMS = "零壹贰叁肆伍陆柒捌玖".toCharArray();
private static final String[] UNITS = {"元", "角", "分", "整","厘","毫"};
private static final String[] U1 = {"", "拾", "佰", "仟"};
private static final String[] U2 = {"", "万", "亿"};
/**
* 将金额(整数部分等于或少于12位)转换为中文大写形式.
* @param amount 金额数字
* @return 中文大写
* @throws IllegalArgumentException
*/
public static String convert(String amount) throws IllegalArgumentException {
// 去掉分隔符
amount = amount.replace(",", "");
double num=Double.parseDouble(amount);
amount=String.format("%.2f",num);
// 验证金额正确性
if (amount.equals("0.00")) {
throw new IllegalArgumentException("金额不能为零.");
}
Matcher matcher = AMOUNT_PATTERN.matcher(amount);
if (!matcher.find()) {
throw new IllegalArgumentException("输入金额有误.");
}
String trigger=matcher.group(1);
String integer = matcher.group(2); // 整数部分
String fraction = matcher.group(3); // 小数部分
String result = "";
if(trigger.equals("-")){
result += "负";
}
if (!integer.equals("0")) {
result += integer2rmb(integer) + UNITS[0]; // 整数部分
}
if (fraction.matches("0*")) {
result += UNITS[3]; // 添加[整]
} else if (fraction.startsWith("0") && integer.equals("0")) {
result += fraction2rmb(fraction).substring(1); // 去掉分前面的[零]
} else {
result += fraction2rmb(fraction); // 小数部分
}
return result;
}
// 将金额小数部分转换为中文大写
private static String fraction2rmb(String fraction) {
StringBuffer sb=new StringBuffer();
char jiao='0';
char fen='0';
char li='0';
char hao='0';
int length=fraction.length();
if(length>0){
jiao = fraction.charAt(0); // 角
}
if(length>1){
fen = fraction.charAt(1); // 分
}
if(length>2){
li=fraction.charAt(2);
}
if(length>3){
hao=fraction.charAt(3);
}
return (RMB_NUMS[jiao - '0'] + (jiao > '0' ? UNITS[1] : ""))
+ (fen > '0' ? RMB_NUMS[fen - '0'] + UNITS[2] : "")
+ (li>'0'?RMB_NUMS[li-'0']+UNITS[4]:"")
+ (hao>'0'?RMB_NUMS[hao-'0']+UNITS[5]:"");
}
// 将金额整数部分转换为中文大写
private static String integer2rmb(String integer) {
StringBuilder buffer = new StringBuilder();
// 从个位数开始转换
int i, j;
for (i = integer.length() - 1, j = 0; i >= 0; i--, j++) {
char n = integer.charAt(i);
if (n == '0') {
// 当n是0且n的右边一位不是0时,插入[零]
if (i < integer.length() - 1 && integer.charAt(i + 1) != '0') {
buffer.append(RMB_NUMS[0]);
}
// 插入[万]或者[亿]
if (j % 4 == 0) {
if (i > 0 && integer.charAt(i - 1) != '0'
|| i > 1 && integer.charAt(i - 2) != '0'
|| i > 2 && integer.charAt(i - 3) != '0') {
buffer.append(U2[j / 4]);
}
}
} else {
if (j % 4 == 0) {
buffer.append(U2[j / 4]); // 插入[万]或者[亿]
}
buffer.append(U1[j % 4]); // 插入[拾]、[佰]或[仟]
buffer.append(RMB_NUMS[n - '0']); // 插入数字
}
}
return buffer.reverse().toString();
}
public static void main(String[] args){
System.out.println(Amount2RMB.convert("-20.09"));
}
}
相关文章推荐
- 汉字字符串转换成十六进制byte数组,一个汉字存到两个byte里面,大整数存到两个byte里面
- delphi 怎么将一个流转换成字符串?或者将字符串转出一个流
- 有一个字符串s,t,d,g,h,m怎么把他转换成数组然后排序!(split的使用)
- delphi 怎么将一个流转换成字符串?
- c c++怎么判断一个字符串中是否含有汉字
- c c++怎么判断一个字符串中是否含有汉字
- delphi 怎么将一个流转换成字符串?
- delphi 怎么将一个文件流转换成字符串(String到流,String到文件,相互转化)
- c#下怎么判断一个字符串是否可以转换为double类型
- 输入一个汉字字符串'我爱中国',怎么返回他的首拼'WAZG'
- 怎么判断一个字符串是整型?不能用正则表达式,也不能用convert强制转换,更不能用Parsint
- 计算字符串的长度(一个汉字算两个字符)
- 转换字符串中汉字为其拼音缩写(C#)
- C#实现将汉字与英文字符混和字符串中的汉字转换为拼音
- 将字符串中连续的空格转换为一个空格
- 输入一个字符串,把其中大写字母转换成小写字母
- 读取字符串的长度,一个汉字,为两个字节
- 给定一个一oracle建表的sql语句,解析读出的字符串,将表名、字段名、字段类型数据取出,怎么实现啊?
- 转换字符串中汉字为其拼音缩写(C#)
- 让一个ComboBox的显示字符串以给定的颜色显示,怎么做?