早教吧作业答案频道 -->其他-->
java如何将数字转换为英文比如将123转换为onethousandtwohundredthirtyfour
题目详情
java如何将数字转换为英文
比如将123转换为one thousand two hundred thirty four
比如将123转换为one thousand two hundred thirty four
▼优质解答
答案和解析
Java 数字转成英文
英文数词,按3位3位区分
Hundred: 100
Thousand:1,000
Million: 1,000,000
Billion: 1,000,000,000
Trillion: 1,000,000,000,000
Quintillion: 1,000,000,000,000,000,000
Sextillion: 1,000,000,000,000,000,000,000
Nonillion: 1,000,000,000,000,000,000,000,000,000,000
Centillion: 1 followed by 303 zeros
所以把数字字符串按3位分割,分别解析
public class NumUtil {
public static final String[] enNum = { // 基本数词表
"zero", "one", "tow", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
"twenty", "", "", "", "", "", "", "", "", "", "thirty", "", "", "",
"", "", "", "", "", "", "fourty", "", "", "", "", "", "", "", "",
"", "fifty", "", "", "", "", "", "", "", "", "", "sixty", "", "",
"", "", "", "", "", "", "", "seventy", "", "", "", "", "", "", "",
"", "", "eighty", "", "", "", "", "", "", "", "", "", "ninety" };
public static final String[] enUnit = { "hundred", "thousand", "million",
"billion", "trillion", "quintillion" }; // 单位表
public static void main(String[] args) {
System.out.println(analyze(1)); // 测试数据
System.out.println(analyze(21));
System.out.println(analyze(105));
System.out.println(analyze(3250));
System.out.println(analyze(47826));
System.out.println(analyze(56945781));
}
public static String analyze(long num) { // long型参数,
return analyze(String.valueOf(num)); // 因为long型有极限,所以以字符串参数方法为主
}
public static String analyze(String num) { // 数字字符串参数
// 判断字符串是否为数字
if (!num.matches("\\d+")) {
return String.format("%s is not number", num);
}
num = num.replaceAll("^[0]*([1-9]*)", "$1"); // 把字符串前面的0去掉
if (num.length() == 0) { // 如果长度为0,则原串都是0
return enNum[0];
} else if (num.length() > 9) { // 如果大于9,即大于999999999,题目限制条件
return "too big";
}
// 按3位分割分组
int count = (num.length() % 3 == 0) ? num.length() / 3
: num.length() / 3 + 1;
if (count > enUnit.length) {
return "too big";
} // 判断组单位是否超过,
// 可以根据需求适当追加enUnit
String[] group = new String[count];
for (int i = num.length(), j = group.length - 1; i > 0; i -= 3) {
group[j--] = num.substring(Math.max(i - 3, 0), i);
}
StringBuilder buf = new StringBuilder(); // 结果保存
for (int i = 0; i < count; i++) { // 遍历分割的组
int v = Integer.valueOf(group[i]);
if (v >= 100) { // 因为按3位分割,所以这里不会有超过999的数
buf.append(enNum[v / 100]).append(" ").append(enUnit[0])
.append(" ");
v = v % 100; // 获取百位,并得到百位以后的数
if (v != 0) {
buf.append("and ");
} // 如果百位后的数不为0,则追加and
}
if (v != 0) { // 前提是v不为0才作解析
if (v < 20 || v % 10 == 0) { // 如果小于20或10的整数倍,直接取基本数词表的单词
buf.append(enNum[v]).append(" ");
} else { // 否则取10位数词,再取个位数词
buf.append(enNum[v - v % 10]).append(" ");
buf.append(enNum[v % 10]).append(" ");
}
if (i != count - 1) { // 百位以上的组追加相应的单位
buf.append(enUnit[count - 1 - i]).append(" ");
}
}
}
return buf.toString().trim(); // 返回值
}
}
英文数词,按3位3位区分
Hundred: 100
Thousand:1,000
Million: 1,000,000
Billion: 1,000,000,000
Trillion: 1,000,000,000,000
Quintillion: 1,000,000,000,000,000,000
Sextillion: 1,000,000,000,000,000,000,000
Nonillion: 1,000,000,000,000,000,000,000,000,000,000
Centillion: 1 followed by 303 zeros
所以把数字字符串按3位分割,分别解析
public class NumUtil {
public static final String[] enNum = { // 基本数词表
"zero", "one", "tow", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
"twenty", "", "", "", "", "", "", "", "", "", "thirty", "", "", "",
"", "", "", "", "", "", "fourty", "", "", "", "", "", "", "", "",
"", "fifty", "", "", "", "", "", "", "", "", "", "sixty", "", "",
"", "", "", "", "", "", "", "seventy", "", "", "", "", "", "", "",
"", "", "eighty", "", "", "", "", "", "", "", "", "", "ninety" };
public static final String[] enUnit = { "hundred", "thousand", "million",
"billion", "trillion", "quintillion" }; // 单位表
public static void main(String[] args) {
System.out.println(analyze(1)); // 测试数据
System.out.println(analyze(21));
System.out.println(analyze(105));
System.out.println(analyze(3250));
System.out.println(analyze(47826));
System.out.println(analyze(56945781));
}
public static String analyze(long num) { // long型参数,
return analyze(String.valueOf(num)); // 因为long型有极限,所以以字符串参数方法为主
}
public static String analyze(String num) { // 数字字符串参数
// 判断字符串是否为数字
if (!num.matches("\\d+")) {
return String.format("%s is not number", num);
}
num = num.replaceAll("^[0]*([1-9]*)", "$1"); // 把字符串前面的0去掉
if (num.length() == 0) { // 如果长度为0,则原串都是0
return enNum[0];
} else if (num.length() > 9) { // 如果大于9,即大于999999999,题目限制条件
return "too big";
}
// 按3位分割分组
int count = (num.length() % 3 == 0) ? num.length() / 3
: num.length() / 3 + 1;
if (count > enUnit.length) {
return "too big";
} // 判断组单位是否超过,
// 可以根据需求适当追加enUnit
String[] group = new String[count];
for (int i = num.length(), j = group.length - 1; i > 0; i -= 3) {
group[j--] = num.substring(Math.max(i - 3, 0), i);
}
StringBuilder buf = new StringBuilder(); // 结果保存
for (int i = 0; i < count; i++) { // 遍历分割的组
int v = Integer.valueOf(group[i]);
if (v >= 100) { // 因为按3位分割,所以这里不会有超过999的数
buf.append(enNum[v / 100]).append(" ").append(enUnit[0])
.append(" ");
v = v % 100; // 获取百位,并得到百位以后的数
if (v != 0) {
buf.append("and ");
} // 如果百位后的数不为0,则追加and
}
if (v != 0) { // 前提是v不为0才作解析
if (v < 20 || v % 10 == 0) { // 如果小于20或10的整数倍,直接取基本数词表的单词
buf.append(enNum[v]).append(" ");
} else { // 否则取10位数词,再取个位数词
buf.append(enNum[v - v % 10]).append(" ");
buf.append(enNum[v % 10]).append(" ");
}
if (i != count - 1) { // 百位以上的组追加相应的单位
buf.append(enUnit[count - 1 - i]).append(" ");
}
}
}
return buf.toString().trim(); // 返回值
}
}
看了 java如何将数字转换为英文...的网友还看了以下:
变限积分求道问题对函数f(t+h)-f(t-h)在[-h,h]上的积分对h求导.F(h)=∫[-h 2020-05-23 …
信号与系统关于拉氏变换的一道题.求x(t)=δ(3t)+u(3t)的拉氏变换.δ(at)=1/a* 2020-06-06 …
已知微分方程(y'')*[(1+x^2)]^2=y;通过变换y=u(t)sect;x=tant,将 2020-06-12 …
m(t)=[u(t)-u(t-t0/3)]+2[u(t-t0/3)-u(t-2t0/3)]杂求它的 2020-07-13 …
变上限积分求导?x∫(0到x)f(x-t)dt求导把x-t换成u我求的是xf(u)+∫(0到x)f 2020-07-14 …
复合函数求导时,为什么不能把中间变量直接带进去例如:y=(3x-1)^5,求导可以引入中间变量U= 2020-07-20 …
simulink中的s函数我用simulink搭建了一个模块,用到了s函数,用来实现以下功能:对于 2020-07-23 …
一个很简答的拉普拉斯变换数学问题请问,t[u(t)-u(t-a)],是如何计算出3个函数的,这个式子 2020-12-31 …
积分变换中的u(t)e^(-at)*u(t)的fourier变换等于e^(-at)的然而sin(wt 2021-01-07 …
求fourier变换f(t)=u(t)(e^-βt)sinω0t(β>0)f(t)=u(t)(e^- 2021-01-07 …