英文:
How to Convert Numbers to words
问题
String numberInWords(double numbers) {
String result = "";
String[] digits = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
String numberAsString = String.valueOf(numbers);
int decimalIndex = numberAsString.indexOf('.');
String wholePart = numberAsString.substring(0, decimalIndex);
String decimalPart = numberAsString.substring(decimalIndex + 1);
for (int i = 0; i < wholePart.length(); i++) {
int digit = Character.getNumericValue(wholePart.charAt(i));
result += digits[digit];
if (i < wholePart.length() - 1) {
result += " ";
}
}
result += " point ";
for (int i = 0; i < decimalPart.length(); i++) {
int digit = Character.getNumericValue(decimalPart.charAt(i));
result += digits[digit];
if (i < decimalPart.length() - 1) {
result += " ";
}
}
return result;
}
英文:
Currently I'm attempting to convert a number to a word, but I'm unable to get the result that I want. Basically, my question is if I can convert a double to a string, the number converted into words, one for each decimal number, including the decimal points.
This is my method
String numberInWords (double numbers){
String result = "" + numbers;
for (int i = 0; i < result.length(); i++) {
if( i == 0) {
result += "zero";
}
if(i == 1) {
result += "one";
}
if(i == 2) {
result += "two";
}
if(i == 3) {
result += "three";
}
if(i == 4) {
result += "four";
}
if(i == 5) {
result += "five";
}
if(i == 6) {
result += "six";
}
if(i == 7) {
result += "seven";
}
if(i == 8) {
result += "eight";
}
if(i == 9) {
result += "nine";
}
}
return result;
}
and the result :
Expected :two three
Actual :23.0zeroonetwothreefourfivesixseveneightnine
答案1
得分: 1
有更加优雅的方法来完成这个任务,但我选择了一种影响你的代码最小的方式。
所以,如果你真的想要一个双精度浮点数作为输入,你还希望能够将23L显示为“two three point zero”,因为它等于23.0,如果不需要的话,你可以调整我的代码片段,但这就是它为你提供的功能。
String numberInWords(double numbers) {
char[] numberInCharacters = String.valueOf(numbers).toCharArray();
String result = "";
for (int i = 0; i < numberInCharacters.length; i++) {
if (i != 0) {
result += " ";
}
if (numberInCharacters[i] == '0') {
result += "zero";
} else if (numberInCharacters[i] == '1') {
result += "one";
} else if (numberInCharacters[i] == '2') {
result += "two";
} else if (numberInCharacters[i] == '3') {
result += "three";
} else if (numberInCharacters[i] == '4') {
result += "four";
} else if (numberInCharacters[i] == '5') {
result += "five";
} else if (numberInCharacters[i] == '6') {
result += "six";
} else if (numberInCharacters[i] == '7') {
result += "seven";
} else if (numberInCharacters[i] == '8') {
result += "eight";
} else if (numberInCharacters[i] == '9') {
result += "nine";
} else if (numberInCharacters[i] == '.') {
result += "point";
}
}
return result;
}
如果你只想要整数部分,你可以将双精度浮点数转换为整数,丢弃小数部分,然后继续将其转化为字符串的过程。
英文:
There are much more elegant ways to have this done, but I did it in a way that affects your code the least.
So, if you really want to have a double as an input, you will also want to enable to have 23L being printed as "two three point zero" because it is 23.0, if not, you can tweak my snippet, but that is what it will provide for you.
String numberInWords (double numbers){
char[] numberInCharacters = String.valueOf(numbers).toCharArray();
String result = "";
for (int i = 0; i < numberInCharacters.length; i++) {
if(i != 0) {
result += " ";
}
if( numberInCharacters[i] == '0') {
result += "zero";
}
else if(numberInCharacters[i] == '1') {
result += "one";
}
else if(numberInCharacters[i] == '2') {
result += "two";
}
else if(numberInCharacters[i] == '3') {
result += "three";
}
else if(numberInCharacters[i] == '4') {
result += "four";
}
else if(numberInCharacters[i] == '5') {
result += "five";
}
else if(numberInCharacters[i] == '6') {
result += "six";
}
else if(numberInCharacters[i] == '7') {
result += "seven";
}
else if(numberInCharacters[i] == '8') {
result += "eight";
}
else if(numberInCharacters[i] == '9') {
result += "nine";
}
else if (numberInCharacters[i] == '.') {
result += "point";
}
}
return result;
}
If you only want predecimal part then you can transform the double to int and lose the decimal part then continue with the process to stringify it.
答案2
得分: 0
尽管 kiselica-aldin 提供的代码可以正常工作,但它并未解决大部分问题。它还会引入一个 bug,因为最后的空白字符未被移除。
我已经添加了注释来解释代码的各个部分。
public String numberInWords(double numbers) {
String numbersAsString = String.valueOf(numbers); // 使用 String.valueOf(numbers) 不是最佳方法
StringBuilder result = new StringBuilder(); // 使用 StringBuilder 而不是字符串拼接
// 我们可以直接迭代字符列表,无需先迭代数字,然后每次访问 char[]
for (char digit : numbersAsString.toCharArray()) {
if (digit == '0') {
result.append("zero "); // 添加尾随空格,因为您想要它们分开
continue; // 如果已经有结果,为什么还要执行其他的 if 语句呢?
}
if (digit == '1') {
result.append("one ");
continue;
}
// 其他数字的判断类似,以此类推
if (digit == '.') {
result.append("dot "); // 或者逗号,或者其他,因为 1.25 和 125 之间有很大区别
// 这里不需要继续,因为它已经是最后一个了
}
}
return result.toString().trim(); // 使用 trim 去除最后的空格
}
(注意:由于您要求只返回翻译好的部分,我已经省略了原始英文内容以及非代码部分的翻译。)
英文:
Although the code provided by kiselica-aldin will work ok, it doesn't solve most of the issues. It will also add a bug, since the last whitespace is never removed.
I've left comments to explain parts of the code.
public String numberInWords (double numbers){
String numbersAsString = String.valueOf(numbers); // "" + numbers isn't the best way of doing it
StringBuilder result = new StringBuilder(); //string builder is better that string concatenation
//we can iterate through the char list directly, no need to iterate through numbers and then access the char[] every time
for (char digit : numbersAsString.toCharArray()) {
if( digit == '0') {
result.append("zero "); //adding a trailing space, since you want them separated
continue; //what is the point of doing the other if statements if we already have a result?
}
if( digit == '1') {
result.append("one ");
continue;
}
if( digit == '2') {
result.append("two ");
continue;
}
if( digit == '3') {
result.append("three ");
continue;
}
if( digit == '4') {
result.append("four ");
continue;
}
if( digit == '5') {
result.append("five ");
continue;
}
if( digit == '6') {
result.append("six ");
continue;
}
if( digit == '7') {
result.append("seven ");
continue;
}
if( digit == '8') {
result.append("eight ");
continue;
}
if( digit == '9') {
result.append("nine ");
continue;
}
if( digit == '.') {
result.append("dot "); //or comma, or whatever, since there is a big difference between 1.25 and 125
//nothing to continue since it already is the last one
}
}
return result.toString().trim(); //trim to remove the last space
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论