Java 如何将数字转换为单词

Java 如何将数字转换为单词,java,Java,目前我正在尝试将一个数字转换为一个单词,但我无法得到我想要的结果。基本上,我的问题是,我是否可以将一个double转换为一个字符串,将数字转换为单词,每个十进制数对应一个,包括小数点 这是我的方法 String numberInWords (double numbers){ String result = "" + numbers; for (int i = 0; i < result.length(); i++) { if( i ==

目前我正在尝试将一个数字转换为一个单词,但我无法得到我想要的结果。基本上,我的问题是,我是否可以将一个double转换为一个字符串,将数字转换为单词,每个十进制数对应一个,包括小数点

这是我的方法

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;

}

有很多更优雅的方法可以做到这一点,但我的方法对代码的影响最小

所以,如果你真的想要一个double作为输入,你还需要启用23L打印为“两三点零”,因为它是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;
    }
String numberInWords(双倍数字){
char[]numberInCharacters=String.valueOf(numbers.tocharray();
字符串结果=”;
for(int i=0;i

如果您只需要小数点前的部分,那么可以将double转换为int,并丢失小数部分,然后继续进行字符串化处理。

尽管kiselica aldin提供的代码可以正常工作,但它不能解决大多数问题。它还将添加一个bug,因为最后一个空格永远不会被删除

我留下了一些注释来解释部分代码

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
    }

首先,您不应该使用
String result=“”+数字,第二,您比较的是
i
而不是数字中的数字。这是否回答了您的问题?你也可以这么说:我不认为傻瓜是OP想要的。被骗者将整个数字作为一个数字覆盖,而不是单独的数字(就像这里所问的那样)。你的代码有很多错误。例如,在反复更改字符串的同时对字符串进行迭代是一种非常糟糕的方法。你应该先退一步问问自己:我如何找到数字中的数字(或数字的字符作为字符串)?如何从数字1到字符串“1”等等。最后一个空格会发生什么变化?为什么不直接遍历char[],而不是在循环的每次迭代中访问它10次呢?为什么继续使用字符串连接(在循环中)而不是StringBuilder?为什么每次都执行每个
if语句?如果你有一个100位数的数字,你会检查1100个
If语句,可以吗?就像我提到的,有很多更优雅的解决方案,我注意到这是这个参与者在stackoverflow中唯一的交互,这意味着他/她可能是一个新手。虽然你的建议完全正确,但我的目标是尽可能少地进行更改,以提供所要求的结果。我不同意,新手应该从一开始就被告知最佳实践,如果你从错误的学习开始,学习一门语言有什么意义?当他们最终需要编写正确的代码时,他们将有一个随机错误的基础。我也强烈反对您的代码提供所要求的结果,因为后面的空格是一个等待咬人的bug(不是在这个代码中,特别是如果它只是打印到控制台,但一般来说),我已经找到了一个解决方案,使用格式化数据,我感谢你们的帮助,因为你们给我的答案也给了我一些如何完成最终代码的想法!
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
    }