今天是個好日子。我的代碼有問題。我正在嘗試制作一個簡單的程序,將數字轉換為單詞,但我在代碼中遇到了以下問題。$num = 900.00;$exp = explode('.', $num);$f = new NumberFormatter("en_US", NumberFormatter::SPELLOUT);$num_words = ucfirst($f->format($exp[0])) . ' point ' . ucfirst($f->format($exp[1]));我期待的輸出九百讀這是我得到的九百點零這是錯誤注意:未定義的偏移量:1有人可以幫我弄這個嗎。我正在努力尋找答案。謝謝大家
1 回答

慕森王
TA貢獻1777條經驗 獲得超3個贊
您需要if else像這樣使用來檢查點后面的數字。如果為零或小于 1,則不要使用point單詞
$num = floatval(900.00);
$exp = explode('.', $num);
$f = new NumberFormatter("en_US", NumberFormatter::SPELLOUT);
// check the number if the number behind point/comma is less than 1
if(count($exp) == 1){
$num_words = ucfirst($f->format($exp[0]));
}else{
// other than that print with point
$num_words = ucfirst($f->format($exp[0])) . ' point ' . ucfirst($f->format($exp[1]));
}
- 1 回答
- 0 關注
- 141 瀏覽
添加回答
舉報
0/150
提交
取消