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

phpで科学記法の文字列を10進法の数値に変換する

Posted at

さっき会社で

phpで1.234e+5を123400に変換するにはどうしたらいい?

というのを聞かれて調べたのでメモ。
とりあえずstrtoint的なものがあるんじゃないかな?と思いぐぐるとintval()が出てきたので実際試すと

[$] <> php -r 'echo intval("1.234e+5");'
1

駄目。
こういう記法を科学記法と言うらしいことを聞いたので、php 科学記法とかでぐぐると sprintf() が出てきた。聞いてきた人が試したけど駄目だったって言ってたけど一応やってみる。

[$] <> php -r 'echo sprintf("%e", "1.234e+5");'
1.234000e+5

いや、そうじゃない、それじゃない。
そんで他に適当にぐぐっても確かに出てこない。で、phpだしよしなにキャストしてくれるんちゃうと思ってfloatにキャストしてみた。

[$] <> php -r 'echo (float)"1.234e+5";'
123400

bingo!

確かにぐぐって出てこなかったら分からんかもなーっておもた。

0
0
2

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?