每當需要將給定的浮點數向上或向下舍入到最接近的整數時,我們都會使用 PHP 中稱為舍入函數的函數。它需要三個參數,即數量、精度和模式。其中 number 是已被上捨入或向下捨去到最接近的整數的浮點數。 Precision表示要捨入的小數位數,該參數是可選的,mode是指定舍入模式的常數,該參數也是可選的,其值可以是PHP_ROUND_HALF_UP、PHP_ROUND_HALF_DOWN、PHP_ROUND_HALF_EVEN或PHP_ROUND_HALF_ODD。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
在 PHP 中宣告舍入函數的語法:
round(number, precision, mode)
哪裡,
以下是 PHP 回合的範例:
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>
輸出:
在上面的程式中,round 函數用於將給定的浮點整數四捨五入到最接近的整數,最多三位小數,並儲存在名為 a、b 和 c 的變數中。然後,將給定浮點整數四捨五入到最接近的整數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(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>
輸出:
在上面的程式中,round 函數用於將給定的浮點整數四捨五入到最接近的整數,最多三位小數,並儲存在名為 a、b 和 c 的變數中。然後,將給定浮點整數四捨五入到最接近的整數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>
輸出:
在上面的程式中,round 函數用於將給定的浮點整數四捨五入到最接近的整數,最多三位小數,並儲存在名為 a、b 和 c 的變數中。然後,將給定浮點整數四捨五入到最接近的整數2(最高可達舍入函數中指定的小數值)後的結果將以輸出顯示在螢幕上。
以上是PHP回合的詳細內容。更多資訊請關注PHP中文網其他相關文章!