さっき会社で
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!
確かにぐぐって出てこなかったら分からんかもなーっておもた。