在 Java 中将数字转换为单词
本文介绍如何在 Java 中将数字转换为单词。
有时我们需要将数字转换为文字; 例如,123 应转换为一百二十三。 这可以在 Java 中通过小数或大数来实现。
在 Java 中将四位数转换为单词
首先,我们尝试在 Java 中将四位数转换为单词。 例如,如果数字是 4444,则应将其转换为 4444。
参见示例:
package jiyik;
public class Example {
// function to convert numbers to words
static void ConvertFourDigittoWords(char[] InputNumber) {
// Get the length of a number
int NumberLength = InputNumber.length;
// Basic cases
if (NumberLength == 0) {
System.out.println("The input is an empty string.");
return;
}
if (NumberLength > 4) {
System.out.println("The Length of the input number is more than 4 digits.");
return;
}
// create an array for numbers in words; the first string will not be used.
String[] Single_Numbers = new String[] {
"Zero", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine"
};
// create an array for numbers in word; the first string will not be used.
String[] Two_Numbers = new String[] {
"", "Ten", "Eleven", "Twelve",
"Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen"
};
//create arrays for numbers in words; the first two strings will not be used.
String[] Tens_Numbers = new String[] {
"", "", "Twenty", "Thirty", "Forty",
"Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
};
String[] Hundred_Thousand = new String[] { "Hundred", "Thousand" };
// this is used for debugging purposes only
System.out.print(String.valueOf(InputNumber) + " = ");
// for a single digit number
if (NumberLength == 1) {
System.out.println(Single_Numbers[InputNumber[0] - '0']);
return;
}
// the while loop will be used if the number length is more than 0
int i = 0;
while (i < InputNumber.length) {
// for the first two digits
if (NumberLength >= 3) {
if (InputNumber[i] - '0' != 0) {
System.out.print(Single_Numbers[InputNumber[i] - '0'] + " ");
System.out.print(Hundred_Thousand[NumberLength - 3]
+ " ");
}
--NumberLength;
}
// for the last two digits
else {
// 10-19 will be explicitly handled.
if (InputNumber[i] - '0' == 1) {
int NumebrSum = InputNumber[i] - '0' + InputNumber[i + 1] - '0';
System.out.println(Two_Numbers[NumebrSum]);
return;
}
// the 20 will be explicitly handled
else if (InputNumber[i] - '0' == 2 && InputNumber[i + 1] - '0' == 0) {
System.out.println("twenty");
return;
}
// for the rest of two digit numbers from 20 to 99
else {
int a = (InputNumber[i] - '0');
if (a > 0)
System.out.print(Tens_Numbers[a]
+ " ");
else
System.out.print("");
++i;
if (InputNumber[i] - '0' != 0)
System.out.println(
Single_Numbers[InputNumber[i] - '0']);
}
}
++i;
}
}
// main method
public static void main(String[] args) {
ConvertFourDigittoWords("6542".toCharArray());
ConvertFourDigittoWords("876".toCharArray());
ConvertFourDigittoWords("34".toCharArray());
ConvertFourDigittoWords("9".toCharArray());
ConvertFourDigittoWords("76544".toCharArray());
ConvertFourDigittoWords("".toCharArray());
}
}
上面的代码会将任何最多四位数的数字转换为单词。 查看输出:
6542 = Six Thousand Five Hundred Forty Two
876 = Eight Hundred Seventy Six
34 = Thirty Four
9 = Nine
The Length of the input number is more than 4 digits.
The input is an empty string.
但如果数字超过四位数怎么办? 我们还可以创建一个接受最多 15 位数字的程序。
在 Java 中将 15 位数字转换为单词
在上面的示例中,我们使用“百”和“千”来表示四位数字。 在这个例子中,我们需要使用Trillion、Billion和Million; 让我们尝试实现一个将最多 15 位数字转换为单词的示例。
package jiyik;
public class Example {
static String LongNumberstoWords(long Long_Number)
{
long Number_Limit = 1000000000000L;
long Current_Limit = 0;
long M = 0;
// If the number is zero, return zero
if (Long_Number == 0)
return ("Zero");
// Array for the powers of 10
String Multiplier_Array[] = { "", "Trillion", "Billion",
"Million", "Thousand" };
// Array for 20 numbers
String FirstTwenty_Numbers[] = {
"", "One", "Two", "Three",
"Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Eleven",
"Twelve", "Thirteen", "Fourteen", "Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen"
};
// Array for multiples of ten
String Tens_Numbers[] = { "", "Twenty", "Thirty",
"Forty", "Fifty", "Sixty",
"Seventy", "Eighty", "Ninety" };
// If the number is less than 20.
if (Long_Number < 20L)
return (FirstTwenty_Numbers[(int)Long_Number]);
String Result = "";
for (long x = Long_Number; x > 0; x %= Number_Limit, Number_Limit /= 1000) {
//the current accessible limit
Current_Limit = x / Number_Limit;
// the current multiplier may be bigger than your number
while (Current_Limit == 0) {
// Set x as the remainder obtained when the number was divided by the limit
x %= Number_Limit;
// Shift the multiplier by dividing the limit by 1000
Number_Limit /= 1000;
// Get the current value in hundreds.
Current_Limit = x / Number_Limit;
// multiplier shifting
++M;
}
// Add the hundreds' place if the current hundred is greater than 99,
if (Current_Limit > 99)
Result += (FirstTwenty_Numbers[(int)Current_Limit / 100]
+ " Hundred ");
// Bring the current hundred to tens
Current_Limit = Current_Limit % 100;
// If the value in tens belongs to 1 to 19, add using the FirstTwenty_Numbers
if (Current_Limit > 0 && Current_Limit < 20)
Result
+= (FirstTwenty_Numbers[(int)Current_Limit] + " ");
// If the current limit is now a multiple of 10, pass the tens
else if (Current_Limit % 10 == 0 && Current_Limit != 0)
Result
+= (Tens_Numbers[(int)Current_Limit / 10 - 1] + " ");
// If the value is between 20 to 99, print using the FirstTwenty_Numbers
else if (Current_Limit > 20 && Current_Limit < 100)
Result
+= (Tens_Numbers[(int)Current_Limit / 10 - 1] + " "
+ FirstTwenty_Numbers[(int)Current_Limit % 10]
+ " ");
// shift the multiplier is it has not become less than 1000
if (M < 4)
Result += (Multiplier_Array[(int)++M] + " ");
}
return (Result);
}
public static void main(String args[]) {
long InputNumber = 70000000000121L;
System.out.println(InputNumber+" = "+ LongNumberstoWords(InputNumber) + "\n");
InputNumber = 987654321;
System.out.println(InputNumber+" = "+ LongNumberstoWords(InputNumber) + "\n");
InputNumber = 90807060540001L;
System.out.println(InputNumber+" = "+ LongNumberstoWords(InputNumber) + "\n");
InputNumber = 909090909090909L;
System.out.println(InputNumber+" = "+ LongNumberstoWords(InputNumber) + "\n");
}
}
上面的代码会将长数字转换为 15 位数字。 查看输出:
70000000000121 = Seventy Trillion One Hundred Twenty One
987654321 = Nine Hundred Eighty Seven Million Six Hundred Fifty Four Thousand Three Hundred Twenty One
90807060540001 = Ninety Trillion Eight Hundred Seven Billion Sixty Million Five Hundred Forty Thousand One
909090909090909 = Nine Hundred Nine Trillion Ninety Billion Nine Hundred Nine Million Ninety Thousand Nine Hundred Nine
相关文章
默认 Java 密钥库密码
发布时间:2023/08/05 浏览次数:125 分类:Java
-
本文将引导您更改 Java 密钥库密码。 但在继续之前,我们需要对密钥库有一个基本的了解,所以让我们看一下。Java 中的密钥库
Java 中的 Cacerts 与 Keystore
发布时间:2023/08/05 浏览次数:152 分类:Java
-
本文对 cacerts 和 KeyStore 进行了比较并重点介绍了差异。Java 中的 cacerts 与 KeyStore cacerts 是用于验证对等方的 TrustStore,而 KeyStore 用于验证您自己的身份。
将证书添加到 Java 密钥库
发布时间:2023/08/05 浏览次数:176 分类:Java
-
在这里我们将了解如何将证书添加到Java Keystore。什么是 Java 密钥库? Java中的证书是什么,主要问题是如何将该证书添加到Java Keystore中?
Java 中的线程安全集合
发布时间:2023/08/02 浏览次数:141 分类:Java
-
本文通过 Java 中线程安全的代码示例讨论不同的集合类。Java 中的线程安全集合 在Java中,最流行的线程安全集合类是Vector、Stack、Hashtable、Properties等。
Java 中的漂亮打印 XML
发布时间:2023/08/02 浏览次数:57 分类:Java
-
漂亮地打印 XML 意味着以预期的方式打印 XML 代码。 本教程演示如何在控制台中漂亮地打印 XML 和 XML 文件。在控制台中漂亮地打印 XML 下面的示例演示了如何创建 XML 并在控制台中漂亮地打印它
将 XML 解析为 Java 对象
发布时间:2023/08/02 浏览次数:177 分类:Java
-
本文将教我们如何用 Java 解析 XML 并将其转换为对象。将 XML 解析为 Java 对象 当谈论用 Java 解析 XML 文件时,Java 中提供了一些常见且最常用的解析器。
Java 的 Gitignore 文件
发布时间:2023/08/02 浏览次数:101 分类:Java
-
本文通过示例讨论 Java 的 .gitignore 文件,并列出了手动创建 .gitignore 时要遵循的规则。Java .gitignore 文件概述
在 Java 中创建 SSH 连接
发布时间:2023/08/02 浏览次数:91 分类:Java
-
本文讨论如何打开 SSH 连接,并演示如何使用 JSch 和 Apache Mina SSHD 创建 SSH 连接。在 Java 中创建 SSH 连接