【发布时间】:2016-03-04 13:17:26
【问题描述】:
我遇到了以下问题:我得到了类似的值
2,50
1
2,99
现在我想将它们分成如下两部分:
2 and 50
1 and 00
2 and 99
$euro = explode('.', $item->price)[0];
if(array_key_exists(1, explode('.', $item->price))) {
$cent = explode('.', $item->price)[1];
}
else
{
$cent = "00";
}
这不是我猜我应该这样做;-)
还有一个问题:现在我得到以下值:
2 and 5
1 and 00
2 and 99
但应该是的
2 and 50
1 and 00
2 and 99
【问题讨论】:
-
你为什么要这样做?要获得 50 而不是 5,您需要使用 strval() ,以便将其转换为字符串, number_format() 是否适合您?还是 money_format() ?
-
加0变成50你可以使用
str_pad($cent,2,0) -
我需要这样做,因为网页设计师为 cent-values 添加了一个特殊的类 ;-)
-
您是否将浮点值或字符串作为输入?