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】億単位の数値を綺麗にする。一部漢字表示を使う

Posted at

コード

<?php
foreach ([120000001, 20000000, 20020000, 1020000, 120000,12000, 1001] as $number){
    $oku = floor($number / 100000000);
    $man = floor(($number % 100000000) / 10000);
    $sen = ($number % 10000);
    $result = implode('', array_filter([
        ($oku > 0 ? number_format($oku). '億' : ''),
        ($man > 0 ? number_format($man). '万' : ''),
        ($sen > 0 ? number_format($sen)       : '')
    ])) ?: '0';
    echo $result.PHP_EOL;
}

結果

1億2,000万1
2,000
2,002
102
12
1万2,000
1,001
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?