涉及到数字中文表示的一些工具函数
2004-11-09 15:30
411 查看
package kellerdu.util;
import java.util.Hashtable;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.text.NumberFormat;
import java.util.Currency;
/**
*涉及到数字中文表示的一些工具函数
*
* @version 1.0
*/
public class NumCN {
private static Hashtable hy = null;
private static final String[] san = {"", "拾", "百", "千"};
private static final String[] wan = {"", "万", "亿"};
private static final SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy年M月d日 HH:hh EE", Locale.CHINA);
private static final NumberFormat nf = NumberFormat.getCurrencyInstance(
Locale.CHINA);
private static final String SAMBOL = Currency.getInstance(Locale.CHINA).
getSymbol();
private NumCN() {
}
/**
* isCurrency为true,数字全部用大写表示
* @param isCurrency boolean
* @return NumCN
*/
public static NumCN getInstance(boolean isCurrency) {
hy = new Hashtable();
if (isCurrency) {
hy.put("0", "零");
hy.put("1", "壹");
hy.put("2", "貮");
hy.put("3", "叁");
hy.put("4", "肆");
hy.put("5", "伍");
hy.put("6", "陆");
hy.put("7", "柒");
hy.put("8", "捌");
hy.put("9", "玖");
} else {
hy.put("0", "零");
hy.put("1", "一");
hy.put("2", "二");
hy.put("3", "三");
hy.put("4", "四");
hy.put("5", "五");
hy.put("6", "六");
hy.put("7", "七");
hy.put("8", "八");
hy.put("9", "九");
}
return new NumCN();
}
public static NumCN getInstance() {
return NumCN.getInstance(false);
}
/**
* 货币数字表示
* @param f double
* @return String
* @throws Exception
*/
public String toCurrency(float f) {
String s = nf.format(f);
return s;
}
/**
* 货币大写数字表示
* @param f double
* @return String
* @throws Exception
*/
public String toCNCurrency(double f) throws Exception {
if (f < 0) {
throw new Exception("Can not less then 0!");
}
int i = (int) f;
String s = SAMBOL + translate((long) i) + "元";
double ff = f - i;
if (ff > 0) {
ff *= 10;
long fi = Math.round(ff);
if (ff >= 1) {
if (Math.abs(ff - fi) < 1e-3) {
s += (hy.get(String.valueOf(Math.round(ff))) + "角");
} else {
s += (hy.get(String.valueOf(Math.round(ff) - 1)) + "角");
}
}
if (Math.abs(ff - fi) > 1e-3) {
ff = (ff - (int) ff) * 10;
fi = Math.round(ff);
if (ff >= 1) {
if (Math.abs(ff - fi) < 1e-3) {
s += (hy.get(String.valueOf(Math.round(ff))) + "分");
} else {
s += (hy.get(String.valueOf(Math.round(ff) - 1)) + "分");
}
}
}
}
return s+"整";
}
/**
* 翻译,精度六位
* @param f double
* @return String
*/
public String translate(double f) {
String result = "";
if (f < 0) {
result = "负";
f = Math.abs(f);
}
return result + translate((int) f) + con(f - ((int) f));
}
/**
* 翻译小数部分,精度六位
* @param f double
* @return String
*/
private String con(double f) {
String result = "点";
f = f * 10;
while (f < 1) {
result += "零";
f = f * 10;
}
int i = (int) Math.round(f);
if (i < 10) {
result += hy.get(String.valueOf(i));
} else {
result += hy.get(String.valueOf(9));
} while (Math.abs(f - i) > 1e-6) {
f = (f - (int) f) * 10;
i = (int) Math.round(f);
if (i < 10) {
result += hy.get(String.valueOf(i));
} else {
result += hy.get(String.valueOf(9));
}
}
return result;
}
/**
* 翻译
* @param l long
* @return String
*/
public String translate(long l) {
String result = "";
boolean isFu = l < 0;
l = Math.abs(l);
String s = String.valueOf(l);
int j = 0;
for (int i = s.length(); i > 0; i -= 4) {
int be = i - 4;
be = be < 0 ? 0 : be;
String m = "";
if ((j % 4) == 0) {
if (j != 0) {
//System.out.println(s+":"+be);
if ((j % 8) == 0) {
m = "亿";
} else {
m = "万";
}
}
}
//不能出现8亿万这种情况
if ((m.equals("亿") && result.charAt(0) == '万')) {
result = result.replaceFirst("万", "亿");
m = "";
}
result = qian(s.substring(be, i)) + m + result;
j += 4;
}
if (isFu) {
result = "负" + result;
}
return result;
}
/**
* 翻译9999-0000
* @param s String
* @return String
*/
private String qian(String s) {
String r = "";
char[] cs = new char[1];
boolean find = false;
for (int i = s.length() - 1; i > -1; i--) {
//从右数,找到不是0的一位
if (!find) {
if (s.charAt(i) == '0') {
continue;
} else {
find = true;
}
}
cs[0] = s.charAt(i);
if (cs[0] == '0') {
if (s.charAt(i + 1) == '0' || i == 0) {
continue;
} else {
r = (String) hy.get(new String(cs)) + r;
}
} else {
r = (String) hy.get(new String(cs)) + san[s.length() - 1 - i] +
r;
}
}
if ((!find) && r.equals("")) {
r = (String) hy.get("0");
}
return r;
}
public String formate(Date date) {
return sdf.format(date);
}
}
import java.util.Hashtable;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.text.NumberFormat;
import java.util.Currency;
/**
*涉及到数字中文表示的一些工具函数
*
* @version 1.0
*/
public class NumCN {
private static Hashtable hy = null;
private static final String[] san = {"", "拾", "百", "千"};
private static final String[] wan = {"", "万", "亿"};
private static final SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy年M月d日 HH:hh EE", Locale.CHINA);
private static final NumberFormat nf = NumberFormat.getCurrencyInstance(
Locale.CHINA);
private static final String SAMBOL = Currency.getInstance(Locale.CHINA).
getSymbol();
private NumCN() {
}
/**
* isCurrency为true,数字全部用大写表示
* @param isCurrency boolean
* @return NumCN
*/
public static NumCN getInstance(boolean isCurrency) {
hy = new Hashtable();
if (isCurrency) {
hy.put("0", "零");
hy.put("1", "壹");
hy.put("2", "貮");
hy.put("3", "叁");
hy.put("4", "肆");
hy.put("5", "伍");
hy.put("6", "陆");
hy.put("7", "柒");
hy.put("8", "捌");
hy.put("9", "玖");
} else {
hy.put("0", "零");
hy.put("1", "一");
hy.put("2", "二");
hy.put("3", "三");
hy.put("4", "四");
hy.put("5", "五");
hy.put("6", "六");
hy.put("7", "七");
hy.put("8", "八");
hy.put("9", "九");
}
return new NumCN();
}
public static NumCN getInstance() {
return NumCN.getInstance(false);
}
/**
* 货币数字表示
* @param f double
* @return String
* @throws Exception
*/
public String toCurrency(float f) {
String s = nf.format(f);
return s;
}
/**
* 货币大写数字表示
* @param f double
* @return String
* @throws Exception
*/
public String toCNCurrency(double f) throws Exception {
if (f < 0) {
throw new Exception("Can not less then 0!");
}
int i = (int) f;
String s = SAMBOL + translate((long) i) + "元";
double ff = f - i;
if (ff > 0) {
ff *= 10;
long fi = Math.round(ff);
if (ff >= 1) {
if (Math.abs(ff - fi) < 1e-3) {
s += (hy.get(String.valueOf(Math.round(ff))) + "角");
} else {
s += (hy.get(String.valueOf(Math.round(ff) - 1)) + "角");
}
}
if (Math.abs(ff - fi) > 1e-3) {
ff = (ff - (int) ff) * 10;
fi = Math.round(ff);
if (ff >= 1) {
if (Math.abs(ff - fi) < 1e-3) {
s += (hy.get(String.valueOf(Math.round(ff))) + "分");
} else {
s += (hy.get(String.valueOf(Math.round(ff) - 1)) + "分");
}
}
}
}
return s+"整";
}
/**
* 翻译,精度六位
* @param f double
* @return String
*/
public String translate(double f) {
String result = "";
if (f < 0) {
result = "负";
f = Math.abs(f);
}
return result + translate((int) f) + con(f - ((int) f));
}
/**
* 翻译小数部分,精度六位
* @param f double
* @return String
*/
private String con(double f) {
String result = "点";
f = f * 10;
while (f < 1) {
result += "零";
f = f * 10;
}
int i = (int) Math.round(f);
if (i < 10) {
result += hy.get(String.valueOf(i));
} else {
result += hy.get(String.valueOf(9));
} while (Math.abs(f - i) > 1e-6) {
f = (f - (int) f) * 10;
i = (int) Math.round(f);
if (i < 10) {
result += hy.get(String.valueOf(i));
} else {
result += hy.get(String.valueOf(9));
}
}
return result;
}
/**
* 翻译
* @param l long
* @return String
*/
public String translate(long l) {
String result = "";
boolean isFu = l < 0;
l = Math.abs(l);
String s = String.valueOf(l);
int j = 0;
for (int i = s.length(); i > 0; i -= 4) {
int be = i - 4;
be = be < 0 ? 0 : be;
String m = "";
if ((j % 4) == 0) {
if (j != 0) {
//System.out.println(s+":"+be);
if ((j % 8) == 0) {
m = "亿";
} else {
m = "万";
}
}
}
//不能出现8亿万这种情况
if ((m.equals("亿") && result.charAt(0) == '万')) {
result = result.replaceFirst("万", "亿");
m = "";
}
result = qian(s.substring(be, i)) + m + result;
j += 4;
}
if (isFu) {
result = "负" + result;
}
return result;
}
/**
* 翻译9999-0000
* @param s String
* @return String
*/
private String qian(String s) {
String r = "";
char[] cs = new char[1];
boolean find = false;
for (int i = s.length() - 1; i > -1; i--) {
//从右数,找到不是0的一位
if (!find) {
if (s.charAt(i) == '0') {
continue;
} else {
find = true;
}
}
cs[0] = s.charAt(i);
if (cs[0] == '0') {
if (s.charAt(i + 1) == '0' || i == 0) {
continue;
} else {
r = (String) hy.get(new String(cs)) + r;
}
} else {
r = (String) hy.get(new String(cs)) + san[s.length() - 1 - i] +
r;
}
}
if ((!find) && r.equals("")) {
r = (String) hy.get("0");
}
return r;
}
public String formate(Date date) {
return sdf.format(date);
}
}
相关文章推荐
- 把中文表示的数字转成阿拉伯数字 - java
- RDLC 中文数字转化函数
- Delphi文件操作所涉及的一些函数
- 小明买了一些彩色的气球用绳子串在一条线上,想要装饰房间,每个气球都染上了一种颜色,每个气球的形状都是各不相同的。我们用1到9一共9个数字表示不同的颜色,如12345则表示一串5个颜色各不相同的气球串。但小明希望得到不出现重复颜色的气球串,那么现在小明需要将这个气球串剪成多个较短的气球串,小明一共有多少种剪法?如原气球串12345的一种是剪法是剪成12和345两个气球串。
- 开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。
- 将阿拉伯数字转换成中文大写的函数(javascript)
- 介绍一组中文处理工具函数
- 一些工具函数
- PHP人民币金额数字转中文大写的函数代码
- 一些可能常用的工具函数
- sql 自定义函数返回中文,数字,英文
- 真正完整无误的pb11.5 数字(可小数)转人民币中文函数
- 收藏的一些资源:包括Android sdk国内镜像,开发中涉及到的各种实用工具
- 一些基本函数的用法(中文)
- PHP人民币金额数字转中文大写的函数
- 一些工具函数--object 于 DataTable 相互转换
- jira 涉及到scriptrunner的一些过滤器 提供的函数
- 编了一个简单的把阿拉伯数字转化成中文表示的程序,很粗糙,欢迎大家优化!!!
- JS把数字金额转换成中文大写数字的函数
- 51单片机UART通信[发送接收数字字母中文,控制LED亮灭] 【常用波特率、函数】