>  기사  >  백엔드 개발  >  PHP 라운드

PHP 라운드

WBOY
WBOY원래의
2024-08-29 13:10:16832검색

주어진 부동 소수점 숫자를 가장 가까운 정수로 반올림하거나 내림해야 할 때마다 PHP에서는 round 함수라는 함수를 사용합니다. 숫자, 정밀도, 모드라는 세 가지 매개변수를 사용합니다. 여기서 number는 가장 가까운 정수로 반올림되거나 반내림된 부동 소수점 숫자입니다. 정밀도는 반올림할 소수 자릿수를 의미하며 이 매개변수는 선택사항이고 모드는 반올림 모드를 지정하는 상수이며 이 매개변수도 선택사항이며 값은 PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN 또는 PHP_ROUND_HALF_ODD 중 하나일 수 있습니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

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 함수는 숫자, 정밀도, 모드라는 세 가지 매개변수를 사용합니다.
  • 매개변수 숫자는 가장 가까운 정수로 반올림 또는 내림되는 부동 소수점 숫자입니다.
  • 매개변수 정밀도는 반올림할 소수 자릿수를 나타내며 선택사항입니다.
  • mode 매개변수는 반올림 모드를 지정하는 상수이며 이 매개변수도 선택사항이며 값은 PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN 또는 PHP_ROUND_HALF_ODD일 수 있습니다.

PHP 라운드 예시

다음은 PHP 라운드의 예입니다.

예시 #1

주어진 부동 소수점 정수를 최대 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 함수는 주어진 부동 소수점 정수를 최대 3개의 소수 값까지 가장 가까운 정수로 반올림하는 데 사용되며 a, b, c라는 변수에 저장됩니다. 그런 다음 주어진 부동 소수점 정수를 round 함수에 지정된 소수점 값까지 가장 가까운 정수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 함수는 주어진 부동 소수점 정수를 최대 3개의 소수 값까지 가장 가까운 정수로 반올림하는 데 사용되며 a, b, c라는 변수에 저장됩니다. 그런 다음 주어진 부동 소수점 정수를 round 함수에 지정된 소수점 값까지 가장 가까운 정수2로 반올림한 결과가 화면에 출력으로 표시됩니다.

예시 #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(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 함수는 주어진 부동 소수점 정수를 최대 3개의 소수 값까지 가장 가까운 정수로 반올림하는 데 사용되며 a, b, c라는 변수에 저장됩니다. 그런 다음 주어진 부동 소수점 정수를 round 함수에 지정된 소수점 값까지 가장 가까운 정수2로 반올림한 결과가 화면에 출력으로 표시됩니다.

위 내용은 PHP 라운드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.