Webエンジニア向けプログラミング解説動画をYouTubeで配信中!
▶ チャンネル登録はこちら

【PHP8.x】RoundingMode::PositiveInfinity定数の使い方

PositiveInfinity定数の使い方について、初心者にもわかりやすく解説します。

作成日: 更新日:

基本的な使い方

PositiveInfinity定数は、PHP 8で導入されたRoundingModeクラスの一部として提供される定数です。この定数は、数値を正の無限大方向、つまり常に現在の値よりも大きい方向の整数値へと丸めるモードを指定するために使用されます。

具体的には、NumberFormatterクラスのsetRoundingMode()メソッドなどでこの定数を指定することで、数値の表示形式を設定する際に丸めの挙動を制御することができます。PositiveInfinityモードを選択した場合、小数点以下の値を持つ数値は、常に正の無限大方向へ丸められます。例えば、2.1は3に丸められ、-2.1は-2に丸められます。この丸め方は、負の数に対しても数直線上で右方向、すなわち値が増加する方向へ丸める点が特徴です。この定数は、数値データを常に切り上げて最大値側に寄せたい場合や、特定のビジネス要件に基づいて一貫した丸め処理を適用したい場合に活用されます。これにより、PHPアプリケーションにおける数値の正確な丸め処理を、開発者が意図した通りに実装できるようになります。

構文(syntax)

1$mode = RoundingMode::PositiveInfinity;

引数(parameters)

引数なし

引数はありません

戻り値(return)

RoundingMode

正の無限大を表す RoundingMode クラスの定数です。

サンプルコード

PHP RoundingMode::PositiveInfinity で数値丸める

1<?php
2
3/**
4 * PHP 8: The RoundingMode class defines constants used for specifying
5 * rounding behavior, often with classes like NumberFormatter.
6 *
7 * RoundingMode::PositiveInfinity rounds numbers towards positive infinity.
8 * This means fractional parts are always rounded up to the next integer,
9 * regardless of the sign of the number, making it move towards larger positive values.
10 */
11function demonstratePositiveInfinityRounding(): void
12{
13    echo "--- Demonstrating RoundingMode::PositiveInfinity ---\n";
14
15    // Create a NumberFormatter instance for decimal numbers.
16    $formatter = new NumberFormatter('en_US', NumberFormatter::DECIMAL);
17
18    // Set the rounding mode to PositiveInfinity.
19    $formatter->setAttribute(NumberFormatter::ROUNDING_MODE, RoundingMode::PositiveInfinity);
20
21    // Set maximum fraction digits to 0 to round to the nearest integer.
22    $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 0);
23
24    // Define a set of numbers to test the rounding.
25    $numbersToRound = [
26        1.1,   // Rounds up to 2
27        1.9,   // Rounds up to 2
28        -1.1,  // Rounds up to -1 (towards positive infinity)
29        -1.9,  // Rounds up to -1 (towards positive infinity)
30        0.0,   // Stays 0
31        0.5,   // Rounds up to 1
32        -0.5,  // Rounds up to 0 (towards positive infinity)
33    ];
34
35    foreach ($numbersToRound as $number) {
36        // Format the number using the specified rounding mode.
37        $roundedNumber = $formatter->format($number);
38        echo sprintf("Input: %5s, Rounded (PositiveInfinity): %s\n", $number, $roundedNumber);
39    }
40}
41
42// Execute the demonstration function.
43demonstratePositiveInfinityRounding();
44

PHP 8のRoundingMode::PositiveInfinityは、数値の丸め処理方法を指定する定数です。この定数は、数値を常に「正の無限大方向」へ丸めます。具体的には、小数点以下を切り上げ、元の数値より大きいか等しい整数に調整されます。定数なので引数はなく、丸めモードの値として機能します。

負の数も同様に、小数点以下が切り上げられ、ゼロ方向やより正の方向へ丸められます。例えば、-1.1や-1.9は-1に、-0.5は0になります。

サンプルコードでは、NumberFormatterクラスで利用しています。NumberFormatterのsetAttribute()メソッドでNumberFormatter::ROUNDING_MODEに本定数を設定すると、format()時の丸め処理に適用されます。1.1や1.9は2となるように、常に数値を切り上げ、結果を正の方向へ調整したい場合に役立つ定数です。

RoundingMode::PositiveInfinityは、数値を正の無限大方向へ丸める定数です。このモードでは、正の数は小数点以下を切り上げ、負の数も0に近づく(例:-1.1が-1)ように丸められるため、一般的な四捨五入や単純な切り上げとは異なる挙動を示す点に注意が必要です。この定数は、NumberFormatterクラスのような国際化(i18n)関連機能と組み合わせて使用され、単独では機能しません。サンプルコードのようにNumberFormatter::MAX_FRACTION_DIGITSと合わせて設定することで、指定した桁数での丸め処理が実現されます。また、RoundingModeクラスはPHP 8以降で導入された機能のため、古いPHPバージョンでは利用できない点にご留意ください。

PHP 8: RoundingMode::PositiveInfinity で丸める

1<?php
2
3/**
4 * RoundingMode::PositiveInfinity を使用した数値の丸め処理の例を示します。
5 *
6 * PHP 8 以降で利用可能な RoundingMode::PositiveInfinity は、
7 * 数値を常に「正の無限大方向(数直線上で右方向)」に丸めるモードです。
8 * 例えば、1.1 は 2 に、-1.1 は -1 に丸められます。
9 *
10 * phpstanのような静的解析ツールを使用する際、NumberFormatter::format() メソッドが
11 * 文字列を返すことに注意が必要です。'positive-int' などの整数型が必要な場合は、
12 * 明示的な型変換(例: (int))を行うことで、型安全性を高めることができます。
13 *
14 * @param float $value 丸める対象の浮動小数点数。
15 * @return string 丸められた数値を表す文字列。
16 */
17function roundToPositiveInfinity(float $value): string
18{
19    // NumberFormatter を初期化し、小数点以下を0桁に設定します。
20    $formatter = new NumberFormatter('en_US', NumberFormatter::DECIMAL);
21    $formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, 0);
22    $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, 0);
23
24    // 丸めモードを RoundingMode::PositiveInfinity に設定します。
25    $formatter->setAttribute(NumberFormatter::ROUNDING_MODE, RoundingMode::PositiveInfinity);
26
27    // 数値をフォーマットし、丸められた結果を取得します。
28    return $formatter->format($value);
29}
30
31// サンプル値で関数を実行し、結果を出力します。
32// 出力はすべて文字列になります。
33echo "Input: 1.1, Rounded: " . roundToPositiveInfinity(1.1) . PHP_EOL;  // 結果: 2
34echo "Input: 1.0, Rounded: " . roundToPositiveInfinity(1.0) . PHP_EOL;  // 結果: 1
35echo "Input: -1.1, Rounded: " . roundToPositiveInfinity(-1.1) . PHP_EOL; // 結果: -1
36echo "Input: -0.5, Rounded: " . roundToPositiveInfinity(-0.5) . PHP_EOL; // 結果: 0
37echo "Input: 0.5, Rounded: " . roundToPositiveInfinity(0.5) . PHP_EOL;   // 結果: 1
38echo "Input: -2.9, Rounded: " . roundToPositiveInfinity(-2.9) . PHP_EOL; // 結果: -2
39
40// 結果を整数として利用する例(phpstanなどの静的解析ツールでの型警告に注意)
41$roundedIntExample = (int) roundToPositiveInfinity(1.1); // "2" が 2 (int) に変換されます。
42echo "Rounded 1.1 as int: " . $roundedIntExample . PHP_EOL;

PHP 8から利用できるRoundingMode::PositiveInfinityは、数値を常に「正の無限大方向」に丸めるための定数です。これは、数直線上で常に右方向へ切り上げる挙動を意味し、例えば1.1は2に、-1.1は-1に丸められます。

この定数は、NumberFormatterクラスと組み合わせて数値をフォーマットする際に使用します。サンプルコードのroundToPositiveInfinity関数は、引数として丸めたい浮動小数点数$valueを受け取ります。内部ではNumberFormatterを設定し、RoundingMode::PositiveInfinityを指定して丸め処理を行い、丸められた結果を文字列として返します。

NumberFormatter::format()メソッドは常に文字列を返すため、例えばphpstanのような静的解析ツールを使用する際、もし結果を整数型(positive-intなど)として扱いたい場合は、(int)のように明示的な型変換を行うことで、型安全性を高めることができます。これにより、意図しない型不一致による問題を未然に防ぎやすくなります。

このサンプルコードは、数値を常に「正の無限大方向」に丸めるRoundingMode::PositiveInfinityの使用例を示しています。最も重要な注意点は、NumberFormatter::format()メソッドが常に結果を「文字列」として返すことです。そのため、positive-intのような整数型が必要な場面でそのまま使用すると、PHPStanなどの静的解析ツールで型に関する警告が発生する可能性があります。数値を安全に整数として利用したい場合は、(int)のように明示的な型変換を行うことで、型安全性を確保し、意図しない挙動を防ぐことができます。

関連コンテンツ

関連プログラミング言語