1
1

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 5 years have passed since last update.

小数点以下の桁数を揃えたい時

Posted at

ディレクトリを作成したい時なんかにmicro秒を使いたい時があって
どうやったかのメモ

$time_ar = explode(" ",microtime());
echo "UnixTime:{$time_ar[1]}<br>\n";
echo "MicroTime:{$time_ar[0]}<br>\n";
echo "U+M:".$time_ar[0] + $time_ar[1];

普通にこの様に足してしまうと

UnixTime:1457315839
MicroTime:0.57940100
U+M:1457315839.5794

と出たり

UnixTime:1457315839
MicroTime:0.57900100
U+M:1457315839.579

と出たり

で、桁数が安定せず見た目が良くないので

$time_ar = explode(" ",microtime());
echo "UnixTime:{$time_ar[1]}<br>\n";
echo "MicroTime:{$time_ar[0]}<br>\n";
echo "U+M:".str_pad($time_ar[0] + $time_ar[1],15,'0',STR_PAD_RIGHT);

って感じでやると

UnixTime:1457315839
MicroTime:0.57900100
U+M:1457315839.5790

こうなって桁が揃って見栄えがいい

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?