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の小数演算における切り上げ切捨て問題

0
Last updated at Posted at 2020-06-08

// 小数点下位+1桁 指定での切り捨て処理
$strNum = round($strNum - 0.5 * pow(0.1, $decimalPoint+1), $decimalPoint+1, PHP_ROUND_HALF_UP);
// 小数点下位 指定での切り上げ処理
$strNum = round($strNum + 0.5 * pow(0.1, $decimalPoint), $decimalPoint, PHP_ROUND_HALF_DOWN);

【1.5501】などの小数点下位第三位が0で且つ、第四位が0以上の場合
事前に第四位以下を切り捨てしないと、第三位での切り上げが【1.56】となってしまう。

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?