您的位置:首页 > 其它

涉及到数字中文表示的一些工具函数

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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐