如何将数字转换为文字

huangapple go评论83阅读模式
英文:

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 = &quot;&quot; + numbers;
    for (int i = 0; i &lt; result.length(); i++) {
        if( i == 0) {
            result += &quot;zero&quot;;
        }
        if(i == 1) {
            result += &quot;one&quot;;
        }
        if(i == 2) {
            result += &quot;two&quot;;
        }
        if(i == 3) {
            result += &quot;three&quot;;
        }
        if(i == 4) {
            result += &quot;four&quot;;
        }
        if(i == 5) {
            result += &quot;five&quot;;
        }
        if(i == 6) {
            result += &quot;six&quot;;
        }
        if(i == 7) {
            result += &quot;seven&quot;;
        }
        if(i == 8) {
            result += &quot;eight&quot;;
        }
        if(i == 9) {
            result += &quot;nine&quot;;
        }
    }
   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 = &quot;&quot;;
for (int i = 0; i &lt; numberInCharacters.length; i++) {
if(i != 0) {
result += &quot; &quot;;
}
if( numberInCharacters[i] == &#39;0&#39;) {
result += &quot;zero&quot;;
}
else if(numberInCharacters[i] == &#39;1&#39;) {
result += &quot;one&quot;;
}
else if(numberInCharacters[i] == &#39;2&#39;) {
result += &quot;two&quot;;
}
else if(numberInCharacters[i] == &#39;3&#39;) {
result += &quot;three&quot;;
}
else if(numberInCharacters[i] == &#39;4&#39;) {
result += &quot;four&quot;;
}
else if(numberInCharacters[i] == &#39;5&#39;) {
result += &quot;five&quot;;
}
else if(numberInCharacters[i] == &#39;6&#39;) {
result += &quot;six&quot;;
}
else if(numberInCharacters[i] == &#39;7&#39;) {
result += &quot;seven&quot;;
}
else if(numberInCharacters[i] == &#39;8&#39;) {
result += &quot;eight&quot;;
}
else if(numberInCharacters[i] == &#39;9&#39;) {
result += &quot;nine&quot;;
}
else if (numberInCharacters[i] == &#39;.&#39;) {
result += &quot;point&quot;;
}
}
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); // &quot;&quot; + numbers isn&#39;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 == &#39;0&#39;) {
result.append(&quot;zero &quot;); //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 == &#39;1&#39;) {
result.append(&quot;one &quot;);
continue;
}
if( digit == &#39;2&#39;) {
result.append(&quot;two &quot;);
continue;
}
if( digit == &#39;3&#39;) {
result.append(&quot;three &quot;);
continue;
}
if( digit == &#39;4&#39;) {
result.append(&quot;four &quot;);
continue;
}
if( digit == &#39;5&#39;) {
result.append(&quot;five &quot;);
continue;
}
if( digit == &#39;6&#39;) {
result.append(&quot;six &quot;);
continue;
}
if( digit == &#39;7&#39;) {
result.append(&quot;seven &quot;);
continue;
}
if( digit == &#39;8&#39;) {
result.append(&quot;eight &quot;);
continue;
}
if( digit == &#39;9&#39;) {
result.append(&quot;nine &quot;);
continue;
}
if( digit == &#39;.&#39;) {
result.append(&quot;dot &quot;); //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
}

huangapple
  • 本文由 发表于 2020年10月14日 16:16:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64349247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定