为了账号安全,请及时绑定邮箱和手机立即绑定

如何修复 php 中的拼写错误

如何修复 php 中的拼写错误

PHP
LEATH 2023-08-11 17:05:28
今天是个好日子。我的代码有问题。我正在尝试制作一个简单的程序,将数字转换为单词,但我在代码中遇到了以下问题。$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]));

}


查看完整回答
反对 回复 2023-08-11
  • 1 回答
  • 0 关注
  • 73 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信