0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【PHP】number_format(カンマ)で小数点も表示で余分な小数点の0出さない(.0)出さない

Last updated at Posted at 2023-06-06

コード1

<?php
$num = '1000';
$num = '1000.5';
if ((string)(int)$num === (string)floatval($num)) {
    $result = number_format($num); 
} else {
    $result = number_format($num, 1); 
}
echo $result;

コード2

<?php
$num = '1000.5';
echo str_replace('.0','',number_format($num,1)); 

結果

1,000
1,000.5
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?