Java Program Number to Word

2025 年 5 月 2 日 | 阅读 11 分钟

在本节中,我们将创建一个 Java 程序,该程序可以将给定的数字转换为单词。例如,如果给定的数字是 54,297,则输出应为 Fifty-Four Thousand Two Hundred Ninety-Seven。让我们为之创建一个Java 程序。

NumberToWordExample1.java

输出

Java Program Number to Word

数字不限于四位数。那么如果用户输入超过 5 位数的数字怎么办?上述程序不适用于大位数。

将大数字转换为单词

转换大数字的逻辑完全不同。让我们看看转换大数字的方法。

NumberToWordExample2.java

输出

Java Program Number to Word

将非常长的数字转换为单词

将非常长的数字转换为单词的逻辑与前一个不同。在继续之前,了解大数字的名称很重要。下表描述了大数字的名称。

名称短数制
(美国、加拿大英语、现代英国、澳大利亚和东欧)
长数制
(加拿大法语、旧英国、西欧和中欧)
百万106106
十亿未使用109
十亿1091012
十亿未使用1015
万亿10121018
千万亿10151024
百亿亿10181030
千亿亿10211036
百亿亿10241042
千亿亿10271048
百亿亿10301054
千亿亿10331060
万亿亿10361066
千万亿亿10391072
百亿亿亿10421078
千亿亿亿10451084
百亿亿亿10481090
千亿亿亿10511096
百亿亿亿105410102
千亿亿亿105710108
百亿亿亿106010114
千亿亿亿106310120
亿亿1030310600

让我们创建一个 Java 程序,将非常长的数字转换为单词。

下面的 Java 程序也将负数和小数转换为单词。

NumberToWordExample3.java

输出

0 = zero
4 = four
10 = ten
12 = twelve
100 = one hundred
108 = one hundred eight
299 = two hundred ninety-nine
1000 = one thousand
1003 = one thousand three
2040 = two thousand forty
45213 = forty-five thousand two hundred thirteen
100000 = one hundred thousand
100005 = one hundred thousand five
100010 = one hundred thousand ten
202020 = two hundred two thousand twenty
202022 = two hundred two thousand twenty-two
999999 = nine hundred ninety-nine thousand nine hundred ninety-nine
1000000 = one million
1000001 = one million one
10000000 = ten million
10000007 = ten million seven
99999999 = ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine
9223372036854775807 = nine quintillion two hundred twenty-three quadrillion three hundred seventy-two trillion thirty-six billion eight hundred fifty-four million seven hundred seventy-five thousand eight hundred seven
-9223372036854775808 = minus nine quintillion two hundred twenty-three quadrillion three hundred seventy-two trillion thirty-six billion eight hundred fifty-four million seven hundred seventy-five thousand eight hundred eight
0001.2 = one and two tenth
3.141592 = three and one hundred forty-one thousand five hundred ninety-two millionth

借助上述程序,我们甚至可以转换100位长的数字。