开发者

Strip Price decimals when necessary

开发者 https://www.devze.com 2023-02-07 19:40 出处:网络
This is more of an aesthetic problem, if I were to output two prices in PHP: (the first being 123.45 and the second being 123.00)

This is more of an aesthetic problem, if I were to output two prices in PHP:

(the first being 123.45 and the second being 123.00)

is there a way I can remove the .00 from the second price when necessary (show it only appears as = 123) but have it re开发者_如何学JAVAmain if there are numbers greater than 0 for a price (like the first price?).

Any help would be great, thanks!

TC


what about...

$value = preg_replace('~\.0+$~','',$value);


If you don't want to use (slow) regular expressions, you can use str_replace:

$value = str_replace('.00', '', $value);

note: I'm assuming you don't want to change '123.10' to '123.1', you only want to remove double zeros, right?


function round2($decimal,$places = 2){
  $decimal = round($decimal,$places);
  if (floor($decimal)==$decimal)
    return (string)floor($decimal);
  return $decimal;
}
echo round2(123.45)."<br />".round2(123.00);

Something like that?

0

精彩评论

暂无评论...
验证码 换一张
取 消