2013-06-19 33 views
-2

我想将数字转换为单词,经过一些研究后,我可以成功将数字转换为英文单词。但是,这只适用于整数。我想这些数字与小数英语单词,如转换:将数字转换为带有十进制的字(java)

123.45 - > 123四十五美分

任何解决方案?

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

+4

呃,你已经完成了绝大多数工作!这不应该是耸人听闻的,是吗? – fge

+0

你到目前为止做了什么,解决这个问题...请在你的问题中添加它...不要把你的任务放在这里..它是stackoverflow.com不谷歌.... –

+0

@Cross骨,我我只是说,把你的代码放在我们可以帮助你,仅仅基于你的问题,我们不能帮你。 –

回答

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) 

编辑(如何让cents-只有部分作为双?):

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); 
+0

thx buddy:D它的作品〜 –

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 
相关问题