首页 > 代码库 > java实现 阿拉伯数字转换为汉字数字 算法
java实现 阿拉伯数字转换为汉字数字 算法
package test;
public class NumberFormatTest {
static String[] units = { "", "十", "百", "千", "万", "十万", "百万", "千万", "亿",
"十亿", "百亿", "千亿", "万亿" };
static char[] numArray = { ‘零‘, ‘一‘, ‘二‘, ‘三‘, ‘四‘, ‘五‘, ‘六‘, ‘七‘, ‘八‘, ‘九‘ };
/**
* @param args
*/
public static void main(String[] args) {
int num = 245000006;
String numStr = foematInteger(num);
print("num= " + num + ", convert result: " + numStr);
double decimal = 245006.234206;
print("============================================================");
String decStr = formatDecimal(decimal);
print("decimal= " + decimal + ", decStr: " + decStr);
}
private static String foematInteger(int num) {
char[] val = String.valueOf(num).toCharArray();
int len = val.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
String m = val[i] + "";
int n = Integer.valueOf(m);
boolean isZero = n == 0;
String unit = units[(len - 1) - i];
if (isZero) {
if (‘0‘ == val[i - 1]) {
// not need process if the last digital bits is 0
continue;
} else {
// no unit for 0
sb.append(numArray[n]);
}
} else {
sb.append(numArray[n]);
sb.append(unit);
}
}
return sb.toString();
}
private static String formatDecimal(double decimal) {
String decimals = String.valueOf(decimal);
int decIndex = decimals.indexOf(".");
int integ = Integer.valueOf(decimals.substring(0, decIndex));
int dec = Integer.valueOf(decimals.substring(decIndex + 1));
String result = foematInteger(integ) + "." + formatFractionalPart(dec);
return result;
}
private static String formatFractionalPart(int decimal) {
char[] val = String.valueOf(decimal).toCharArray();
int len = val.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
int n = Integer.valueOf(val[i] + "");
sb.append(numArray[n]);
}
return sb.toString();
}
private static void print(Object arg0) {
System.out.println(arg0);
}
}
public class NumberFormatTest {
static String[] units = { "", "十", "百", "千", "万", "十万", "百万", "千万", "亿",
"十亿", "百亿", "千亿", "万亿" };
static char[] numArray = { ‘零‘, ‘一‘, ‘二‘, ‘三‘, ‘四‘, ‘五‘, ‘六‘, ‘七‘, ‘八‘, ‘九‘ };
/**
* @param args
*/
public static void main(String[] args) {
int num = 245000006;
String numStr = foematInteger(num);
print("num= " + num + ", convert result: " + numStr);
double decimal = 245006.234206;
print("============================================================");
String decStr = formatDecimal(decimal);
print("decimal= " + decimal + ", decStr: " + decStr);
}
private static String foematInteger(int num) {
char[] val = String.valueOf(num).toCharArray();
int len = val.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
String m = val[i] + "";
int n = Integer.valueOf(m);
boolean isZero = n == 0;
String unit = units[(len - 1) - i];
if (isZero) {
if (‘0‘ == val[i - 1]) {
// not need process if the last digital bits is 0
continue;
} else {
// no unit for 0
sb.append(numArray[n]);
}
} else {
sb.append(numArray[n]);
sb.append(unit);
}
}
return sb.toString();
}
private static String formatDecimal(double decimal) {
String decimals = String.valueOf(decimal);
int decIndex = decimals.indexOf(".");
int integ = Integer.valueOf(decimals.substring(0, decIndex));
int dec = Integer.valueOf(decimals.substring(decIndex + 1));
String result = foematInteger(integ) + "." + formatFractionalPart(dec);
return result;
}
private static String formatFractionalPart(int decimal) {
char[] val = String.valueOf(decimal).toCharArray();
int len = val.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
int n = Integer.valueOf(val[i] + "");
sb.append(numArray[n]);
}
return sb.toString();
}
private static void print(Object arg0) {
System.out.println(arg0);
}
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。