如果在十进制之后有一个零,则添加一个零

une*_*eer -1 php

我有定价格式,其中有许多场景我必须处理!

我的基本要求是在小数点后必须有两个值,例如

20.20
100.03
30.34
Run Code Online (Sandbox Code Playgroud)

等等,现在我收到的定价类型是

30.3
25.52 
100.02
Run Code Online (Sandbox Code Playgroud)

等等

现在我要构建的逻辑是,如果小数点后面有两个值,它应该按原样显示它们,不做任何改动.但是如果在小数点之后有一个值,例如30.3它应该添加一个尾随零但只有两位数!这就是我现在拥有的!

  <h6> <?php $starting_from = explode('.',$result_1s['price']) ?>
  $<?=round($starting_from[0])?><?=(is_numeric($result_1s['price']) && floor($result_1s['price']) != $result_1s['price']) ? '.'.$starting_from[1] :'.00' ?>
 </h6>
Run Code Online (Sandbox Code Playgroud)

如何修改上面的代码,它接受我上面解释的场景.提前致谢!

Ale*_*eri 6

你可以number_format()像这样使用:

number_format($result_1s['price'], 2, '.', '');
Run Code Online (Sandbox Code Playgroud)

文档