如何修复 php 中的拼写

How can I fix spellout in php

大家好。我的代码有问题。 我正在尝试制作一个将数字转换为单词的简单程序,但我在下面的代码中遇到了一些问题。

$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]));

我期待

的输出

Nine hundread

这是我得到的

Nine hundred point Zero

这是错误

Notice: Undefined offset: 1

有人可以帮我解决这个问题吗?我正在努力寻找答案。谢谢大家

您需要像这样使用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]));
}