Hướng dẫn php round to int - php làm tròn thành int

❮ Tham khảo toán học PHP

Thí dụ

Số tròn:

echo(round(0.60) . "
");
echo(round(0.50) . "
");
echo(round(0.49) . "
");
echo(round(-4.40) . "
");
echo(round(-4.60));
?>

Hãy tự mình thử »


Định nghĩa và cách sử dụng

Hàm vòng () làm tròn một số điểm nổi.

Mẹo: Để làm tròn một số lên đến số nguyên gần nhất, hãy nhìn vào hàm cle (). To round a number UP to the nearest integer, look at the ceil() function.

Mẹo: Để làm tròn một số xuống số nguyên gần nhất, hãy nhìn vào chức năng Sàn (). To round a number DOWN to the nearest integer, look at the floor() function.


Cú pháp

vòng (số, độ chính xác, chế độ);

Giá trị tham số

Tham sốSự mô tả
con sốYêu cầu. Chỉ định giá trị thành vòng
độ chính xácKhông bắt buộc. Chỉ định số chữ số thập phân để làm tròn đến. Mặc định là 0
cách thứcKhông bắt buộc. Chỉ định một hằng số để chỉ định chế độ làm tròn:
  • PHP_ROUND_HALF_UP - Mặc định. Vòng số lên đến chính xác số thập phân, khi nó ở đó một nửa. Vòng 1,5 đến 2 và -1,5 đến -2
  • PHP_ROUND_HALF_DOWN - Số tròn xuống các vị trí thập phân chính xác, khi nó ở đó một nửa. Vòng 1,5 đến 1 và -1,5 đến -1
  • PHP_ROUND_HALF_EVEN - Số tròn đến các vị trí thập phân chính xác đối với giá trị chẵn tiếp theo
  • PHP_ROUND_HALF_ODD - Số tròn đến các vị trí thập phân chính xác đối với giá trị lẻ tiếp theo


Chi tiết kỹ thuật

Giá trị trở lại:Giá trị tròn
Loại trở lại:Trôi nổi
Phiên bản PHP:4+
PHP Changelog:Php 5.3: Tham số chế độ đã được thêm

Nhiều ví dụ hơn

Thí dụ

Số tròn đến hai số thập phân:

echo(round(4.96754,2) . "
");
echo(round(7.045,2) . "
");
echo(round(7.055,2));
?>

Hãy tự mình thử »

Thí dụ

Định nghĩa và cách sử dụng

Hàm vòng () làm tròn một số điểm nổi.
echo(round(1.5,0,PHP_ROUND_HALF_UP) . "
");
echo(round(-1.5,0,PHP_ROUND_HALF_UP) . "
");

Mẹo: Để làm tròn một số lên đến số nguyên gần nhất, hãy nhìn vào hàm cle ().
echo(round(-1.5,0,PHP_ROUND_HALF_DOWN) . "
");

Mẹo: Để làm tròn một số xuống số nguyên gần nhất, hãy nhìn vào chức năng Sàn ().
echo(round(-1.5,0,PHP_ROUND_HALF_EVEN) . "
");

Cú pháp
echo(round(-1.5,0,PHP_ROUND_HALF_ODD));
?>

Hãy tự mình thử »


❮ Tham khảo toán học PHP


Rustamabd@gmail-you-know-what ¶int is a number of the set ℤ = {..., -2, -1, 0, 1, 2, ...}.

Litbai ¶

  • 6 năm trước
  • Ẩn danh ¶
  • 7 năm trước

18 năm trước

Darkshire ¶s can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation. The negation operator can be used to denote a negative int.

Jacek ¶

Eric ¶

14 năm trước

<?php
$a 
1234// decimal number
$a 0123// octal number (equivalent to 83 decimal)
$a 0o123// octal number (as of PHP 8.1.0)
$a 0x1A// hexadecimal number (equivalent to 26 decimal)
$a 0b11111111// binary number (equivalent to 255 decimal)
$a 1_234_567// decimal number (as of PHP 7.4.0)
?>

pere dot cil tại wanadoo dot fr ¶int literals is as of PHP 7.4.0 (previously, underscores have not been allowed):

decimal     : [1-9][0-9]*(_[0-9]+)*
            | 0

hexadecimal : 0[xX][0-9a-fA-F]+(_[0-9a-fA-F]+)*

octal       : 0[oO]?[0-7]+(_[0-7]+)*

binary      : 0[bB][01]+(_[01]+)*

integer     : decimal
            | hexadecimal
            | octal
            | binary

11 năm trướcint is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned ints. int size can be determined using the constant PHP_INT_SIZE, maximum value using the constant PHP_INT_MAX, and minimum value using the constant 00.

WBCarts tại Juno Dot Com ¶

13453814063 tại 163 dot com ¶int type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the int type will return a float instead.

Dewi tại Dewimorgan dot com

01

JMW254 tại Cornell Dot Edu ¶

02

16 năm trướcint division operator in PHP, to achieve this use the intdiv() function. 03 yields the float 04. The value can be cast to an int to round it towards zero, or the round() function provides finer control over rounding.

05

Chuyển đổi sang số nguyên

Để chuyển đổi một cách rõ ràng một giá trị thành int, hãy sử dụng các diễn viên 06 hoặc 07. Tuy nhiên, trong hầu hết các trường hợp, các diễn viên không cần thiết, vì giá trị sẽ được tự động chuyển đổi nếu toán tử, hàm hoặc cấu trúc điều khiển yêu cầu đối số INT. Một giá trị cũng có thể được chuyển đổi thành int với hàm intval ().int, use either the 06 or 07 casts. However, in most cases the cast is not needed, since a value will be automatically converted if an operator, function or control structure requires an int argument. A value can also be converted to int with the intval() function.

Nếu một tài nguyên được chuyển đổi thành INT, thì kết quả sẽ là số tài nguyên duy nhất được gán cho tài nguyên bằng PHP khi chạy.resource is converted to an int, then the result will be the unique resource number assigned to the resource by PHP at runtime.

Xem thêm Loại tung hứng.

Từ booleans

08 sẽ mang lại 0 (không) và 0o0 sẽ mang lại 0o1 (một). will yield 0 (zero), and 0o0 will yield 0o1 (one).

Từ số điểm nổi

Khi chuyển đổi từ phao sang INT, số sẽ được làm tròn về 0. Kể từ Php 8.1.0, một thông báo từ chối được phát ra khi ngầm chuyển đổi một chiếc phao không tích phân sang INT mất độ chính xác.float to int, the number will be rounded towards zero. As of PHP 8.1.0, a deprecation notice is emitted when implicitly converting a non-integral float to int which loses precision.

0o2

Nếu phao vượt quá ranh giới của INT (thường là 0o3 trên các nền tảng 32 bit và 0o4 trên các nền tảng 64 bit), kết quả không được xác định, vì phao không có đủ độ chính xác để đưa ra kết quả INT chính xác. Không có cảnh báo, thậm chí không một thông báo sẽ được đưa ra khi điều này xảy ra!int (usually 0o3 on 32-bit platforms and 0o4 on 64-bit platforms), the result is undefined, since the float doesn't have enough precision to give an exact int result. No warning, not even a notice will be issued when this happens!

Ghi chú::

Nan và Infinity sẽ luôn bằng không khi được đúc vào Int.int.

Cảnh báo

Không bao giờ sử dụng một phần không xác định cho Int, vì điều này đôi khi có thể dẫn đến kết quả bất ngờ.int, as this can sometimes lead to unexpected results.

0o5

Xem thêm cảnh báo về độ chính xác nổi.

Từ chuỗi

Nếu chuỗi là số hoặc số dẫn thì nó sẽ giải quyết thành giá trị số nguyên tương ứng, nếu không nó được chuyển đổi thành 0 (0).

Từ nullNULL

0o7 luôn được chuyển đổi thành 0 (0). is always converted to zero (0).

Từ các loại khác

Thận trọng

Hành vi chuyển đổi sang INT không được xác định cho các loại khác. Không dựa vào bất kỳ hành vi quan sát nào, vì nó có thể thay đổi mà không cần thông báo trước.int is undefined for other types. Do not rely on any observed behaviour, as it can change without notice.

PHP tại Richardneill Dot org ¶

9 năm trước

0o9

D_N tại Nospam Dot Loryx Dot Com ¶

15 năm trước

0O0

0O1

0O2

0O3

0O4

Dhairya lakhera ¶

4 năm trước

0O5

0O6

0O7

0O8

0O9

0x0

0x1

0x2

0x3

0x4

0x5

0O4

Egwayjen tại gmail dot com

4 năm trước

0x7

Egwayjen tại gmail dot com

Ganlvtech tại qq dot com ¶

0x8

0x9

0b0

0b1

2 năm trước

Ganlvtech tại qq dot com ¶

0b2

0b3

0b4

0b5

2 năm trước

15 năm trước

0b6

Dhairya lakhera ¶

4 năm trước

0b7

Egwayjen tại gmail dot com

Ganlvtech tại qq dot com ¶

0b8

Egwayjen tại gmail dot com

15 năm trước

0b9

_0

_1

0O4

Egwayjen tại gmail dot com

Ganlvtech tại qq dot com ¶

_3

_4

0O4

2 năm trước

15 năm trước

_6

_7

0O4

Dhairya lakhera ¶

15 năm trước

_9

Dhairya lakhera ¶

4 năm trước

<?php
$a 
1234// decimal number
$a 0123// octal number (equivalent to 83 decimal)
$a 0o123// octal number (as of PHP 8.1.0)
$a 0x1A// hexadecimal number (equivalent to 26 decimal)
$a 0b11111111// binary number (equivalent to 255 decimal)
$a 1_234_567// decimal number (as of PHP 7.4.0)
?>
0

<?php
$a 
1234// decimal number
$a 0123// octal number (equivalent to 83 decimal)
$a 0o123// octal number (as of PHP 8.1.0)
$a 0x1A// hexadecimal number (equivalent to 26 decimal)
$a 0b11111111// binary number (equivalent to 255 decimal)
$a 1_234_567// decimal number (as of PHP 7.4.0)
?>
1

<?php
$a 
1234// decimal number
$a 0123// octal number (equivalent to 83 decimal)
$a 0o123// octal number (as of PHP 8.1.0)
$a 0x1A// hexadecimal number (equivalent to 26 decimal)
$a 0b11111111// binary number (equivalent to 255 decimal)
$a 1_234_567// decimal number (as of PHP 7.4.0)
?>
2

<?php
$a 
1234// decimal number
$a 0123// octal number (equivalent to 83 decimal)
$a 0o123// octal number (as of PHP 8.1.0)
$a 0x1A// hexadecimal number (equivalent to 26 decimal)
$a 0b11111111// binary number (equivalent to 255 decimal)
$a 1_234_567// decimal number (as of PHP 7.4.0)
?>
3

<?php
$a 
1234// decimal number
$a 0123// octal number (equivalent to 83 decimal)
$a 0o123// octal number (as of PHP 8.1.0)
$a 0x1A// hexadecimal number (equivalent to 26 decimal)
$a 0b11111111// binary number (equivalent to 255 decimal)
$a 1_234_567// decimal number (as of PHP 7.4.0)
?>
4

<?php
$a 
1234// decimal number
$a 0123// octal number (equivalent to 83 decimal)
$a 0o123// octal number (as of PHP 8.1.0)
$a 0x1A// hexadecimal number (equivalent to 26 decimal)
$a 0b11111111// binary number (equivalent to 255 decimal)
$a 1_234_567// decimal number (as of PHP 7.4.0)
?>
5

<?php
$a 
1234// decimal number
$a 0123// octal number (equivalent to 83 decimal)
$a 0o123// octal number (as of PHP 8.1.0)
$a 0x1A// hexadecimal number (equivalent to 26 decimal)
$a 0b11111111// binary number (equivalent to 255 decimal)
$a 1_234_567// decimal number (as of PHP 7.4.0)
?>
6

<?php
$a 
1234// decimal number
$a 0123// octal number (equivalent to 83 decimal)
$a 0o123// octal number (as of PHP 8.1.0)
$a 0x1A// hexadecimal number (equivalent to 26 decimal)
$a 0b11111111// binary number (equivalent to 255 decimal)
$a 1_234_567// decimal number (as of PHP 7.4.0)
?>
7

0O4

Egwayjen tại gmail dot com

Ganlvtech tại qq dot com ¶

<?php
$a 
1234// decimal number
$a 0123// octal number (equivalent to 83 decimal)
$a 0o123// octal number (as of PHP 8.1.0)
$a 0x1A// hexadecimal number (equivalent to 26 decimal)
$a 0b11111111// binary number (equivalent to 255 decimal)
$a 1_234_567// decimal number (as of PHP 7.4.0)
?>
9

PHP_INT_SIZE0

PHP_INT_SIZE1

PHP_INT_SIZE2

PHP_INT_SIZE3

PHP_INT_SIZE4

0O4

2 năm trước

4 năm trước

PHP_INT_SIZE6

PHP_INT_SIZE7

PHP_INT_SIZE8

PHP_INT_SIZE9

Egwayjen tại gmail dot com

4 năm trước

PHP_INT_MAX0

Egwayjen tại gmail dot com

Ganlvtech tại qq dot com ¶

PHP_INT_MAX1

PHP_INT_MAX2

PHP_INT_MAX3

PHP_INT_MAX4

PHP_INT_MAX5

PHP_INT_MAX6

PHP_INT_MAX7

PHP_INT_MAX8

0O4

2 năm trước

Kuzawinski Dot Marcin_Nospam tại Gmail Dot Com ¶

000

001

002

003

0O4