ホームページ  >  記事  >  バックエンド開発  >  PHPラウンド

PHPラウンド

WBOY
WBOYオリジナル
2024-08-29 13:10:16833ブラウズ

指定された浮動小数点数を最も近い整数に切り上げまたは切り下げる必要がある場合は、PHP のround関数と呼ばれる関数を使用します。これには、数値、精度、モードという 3 つのパラメータが必要です。ここで、number は、最も近い整数に切り上げまたは切り捨てられた浮動小数点数です。精度は四捨五入される小数点以下の桁数を示し、このパラメータはオプションであり、モードは丸めモードを指定する定数であり、このパラメータもオプションであり、その値は PHP_ROUND_HALF_UP、PHP_ROUND_HALF_DOWN、PHP_ROUND_HALF_EVEN、または PHP_ROUND_HALF_ODD のいずれかになります。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

PHP でラウンド関数を宣言する構文:

round(number, precision, mode)

どこ、

  • 数値は、最も近い整数に切り上げまたは切り捨てられる浮動小数点数です。
  • 精度は、四捨五入される小数点以下の桁数を示します。このパラメータはオプションです。
  • Mode は丸めモードを指定する定数で、このパラメータもオプションであり、値は PHP_ROUND_HALF_UP、PHP_ROUND_HALF_DOWN、PHP_ROUND_HALF_EVEN、または PHP_ROUND_HALF_ODD のいずれかになります。

PHPのround関数の仕組み

  • 指定された浮動小数点数を最も近い整数に切り上げまたは切り下げる必要がある場合は、PHP のround関数と呼ばれる関数を使用します。
  • PHP のround 関数は、数値、精度、モードという 3 つのパラメータを取ります。
  • パラメータ番号は、最も近い整数に切り上げまたは切り捨てられる浮動小数点数です。
  • パラメータの精度は、四捨五入される小数点以下の桁数を示します。これはオプションです。
  • パラメータ mode は丸めモードを指定する定数であり、このパラメータもオプションであり、その値は PHP_ROUND_HALF_UP、PHP_ROUND_HALF_DOWN、PHP_ROUND_HALF_EVEN、または PHP_ROUND_HALF_ODD のいずれかになります。

PHP ラウンドの例

以下は PHP ラウンドの例です:

例 #1

指定された浮動小数点整数を 10 進数値 3 桁までの最も近い整数に四捨五入するラウンド関数の動作を説明する PHP プログラム。

コード:

<html>
<body>
<?php
#round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called a
$a = round(6.7654,3);
#round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called b
$b = round(2.98765,3);
#round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called c
$c = round(5.87654,3);
#The result after using the round function is displayed as the output on the screen
echo("The value after the given floating point number is rounded to the nearest integer is: ");
echo $a;
echo "<br>";
echo("The value after the given floating point number is rounded to the nearest integer is: ");
echo $b;
echo "<br>";
echo("The value after the given floating point number is rounded to the nearest integer is: ");
echo $c;
echo "<br>";
?>
</body>
</html>

出力:

PHPラウンド

上記のプログラムでは、round 関数を使用して、指定された浮動小数点整数を 10 進数値 3 桁までの最も近い整数に四捨五入し、a、b、c という変数に格納します。次に、指定された浮動小数点整数を、round 関数で指定された 10 進数値までの最も近い整数 2 に丸めた結果が、出力として画面に表示されます。

例 #2

指定された浮動小数点整数を 10 進数値までの最も近い整数に四捨五入するラウンド関数の仕組みを説明する PHP プログラム。

コード:

<html>
<body>
<?php
#round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called a
$a = round(8.37465,1);
#round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called b
$b = round(1.09456,1);
#round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called c
$c = round(3.87654,1);
#The result after using the round function is displayed as the output on the screen
echo("The value after the given floating point number is rounded to the nearest integer is: ");
echo $a;
echo "<br>";
echo("The value after the given floating point number is rounded to the nearest integer is: ");
echo $b;
echo "<br>";
echo("The value after the given floating point number is rounded to the nearest integer is: ");
echo $c;
echo "<br>";
?>
</body>
</html>

出力:

PHPラウンド

上記のプログラムでは、round 関数を使用して、指定された浮動小数点整数を 10 進数値 3 桁までの最も近い整数に四捨五入し、a、b、c という変数に格納します。次に、指定された浮動小数点整数を、round 関数で指定された 10 進数値までの最も近い整数 2 に丸めた結果が、出力として画面に表示されます。

例 #3

指定された浮動小数点整数を 10 進数値 2 桁までの最も近い整数に四捨五入するラウンド関数の動作を説明する PHP プログラム。

コード:

<html>
<body>
<?php
#round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called a
$a = round(4.273645,2);
#round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called b
$b = round(7.45567,2);
#round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called c
$c = round(9.3778335,2);
#The result after using the round function is displayed as the output on the screen
echo("The value after the given floating point number is rounded to the nearest integer is: ");
echo $a;
echo "<br>";
echo("The value after the given floating point number is rounded to the nearest integer is: ");
echo $b;
echo "<br>";
echo("The value after the given floating point number is rounded to the nearest integer is: ");
echo $c;
echo "<br>";
?>
</body>
</html>

出力:

PHPラウンド

上記のプログラムでは、round 関数を使用して、指定された浮動小数点整数を 10 進数値 3 桁までの最も近い整数に四捨五入し、a、b、c という変数に格納します。次に、指定された浮動小数点整数を、round 関数で指定された 10 進数値までの最も近い整数 2 に丸めた結果が、出力として画面に表示されます。

以上がPHPラウンドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。