-1

我想将数字转换为单词,经过一些研究,我可以成功地将数字转换为英文单词。但是,这仅适用于整数。我想将带小数的数字转换为英文单词,例如:

123.45 -> 123 和 45 美分

任何解决方案?

参考: http: //pastebin.com/BNL1tdPW

4

2 回答 2

5

由于您拥有所有基本功能,我将只提供一个伪代码建议来获得下半部分:

get the cents-only portion as a double. (0.45)
multiply the cents by 100. (45)
use your normal conversion technique to the English words. (Forty Five)

编辑(如何将仅美分部分作为双倍?):

    double money = 123.45;

    int dollars = (int) Math.floor(money);
    double cents = money - dollars;
    int centsAsInt = (int) (100 * cents);

    System.out.println("dollars: " + dollars);
    System.out.println("cents: " + cents);
    System.out.println("centsAsInt: " + centsAsInt);
于 2013-06-19T05:21:34.533 回答
1

使用BigDecimal. 您可以按如下方式获取小数部分:

final BigDecimal intPart = new BigDecimal(orig.toBigInteger);
final BigDecimal fracPart = orig.minus(intPart);
final int scale = fractPart.scale();
final String fractPartAsString = fracPart.mult(BigDecimal.TEN.pow(scale));
// treat fractPartAsString
于 2013-06-19T05:50:29.923 回答