我想將數字轉換爲單詞,經過一些研究後,我可以成功將數字轉換爲英文單詞。但是,這隻適用於整數。我想這些數字與小數英語單詞,如轉換:將數字轉換爲帶有十進制的字(java)
123.45 - > 123四十五美分
任何解決方案?
參考:http://pastebin.com/BNL1tdPW
我想將數字轉換爲單詞,經過一些研究後,我可以成功將數字轉換爲英文單詞。但是,這隻適用於整數。我想這些數字與小數英語單詞,如轉換:將數字轉換爲帶有十進制的字(java)
123.45 - > 123四十五美分
任何解決方案?
參考:http://pastebin.com/BNL1tdPW
當你把所有的基本功能,我將只是一個僞代碼的建議得到下半場:
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);
thx buddy:D它的作品〜 –
使用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
呃,你已經完成了絕大多數工作!這不應該是聳人聽聞的,是嗎? – fge
你到目前爲止做了什麼,解決這個問題...請在你的問題中添加它...不要把你的任務放在這裏..它是stackoverflow.com不穀歌.... –
@Cross骨,我我只是說,把你的代碼放在我們可以幫助你,僅僅基於你的問題,我們不能幫你。 –