Hướng dẫn how do you round a float in php? - làm thế nào để bạn làm tròn một phao trong php?

[Php 4, Php 5, Php 7, Php 8]

Vòng - Vòng trònRounds a float

Sự mô tả

vòng [int | float $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP]: float[int|float $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP]: float

Thông số

________số 8

Giá trị để tròn.

precision

Số lượng các chữ số thập phân tùy chọn để làm tròn đến.

Nếu precision là dương, num được làm tròn đến precision chữ số quan trọng sau điểm thập phân.

Nếu precision là âm, num được làm tròn thành precision Các chữ số đáng kể trước điểm thập phân, tức là với bội số gần nhất của

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
6, ví dụ: Đối với precision của -1 num được làm tròn đến hàng chục, với giá precision từ -2 đến hàng trăm, v.v.

Rounding modes with 9.5
float[10]
float[9]
float[10]
float[9]

Rounding modes with 8.5
float[9]
float[8]
float[8]
float[9]
0

Sử dụng một trong các hằng số sau để chỉ định chế độ xảy ra làm tròn.

Hằng sốSự mô tả
PHP_ROUND_HALF_UP vòng [int | float $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP]: float
Rounding modes with 9.5
float[10]
float[9]
float[10]
float[9]

Rounding modes with 8.5
float[9]
float[8]
float[8]
float[9]
3
Thông số
Rounding modes with 9.5
float[10]
float[9]
float[10]
float[9]

Rounding modes with 8.5
float[9]
float[8]
float[8]
float[9]
5
________số 8
Rounding modes with 9.5
float[10]
float[9]
float[10]
float[9]

Rounding modes with 8.5
float[9]
float[8]
float[8]
float[9]
7
Giá trị để tròn.

precision

Số lượng các chữ số thập phân tùy chọn để làm tròn đến.float.

Nếu precision là dương, num được làm tròn đến precision chữ số quan trọng sau điểm thập phân.

Nếu precision là âm, num được làm tròn thành precision Các chữ số đáng kể trước điểm thập phân, tức là với bội số gần nhất của
float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
6, ví dụ: Đối với precision của -1 num được làm tròn đến hàng chục, với giá precision từ -2 đến hàng trăm, v.v.Sự mô tả
8.0.0
Rounding modes with 9.5
float[10]
float[9]
float[10]
float[9]

Rounding modes with 8.5
float[9]
float[8]
float[8]
float[9]
0

Sử dụng một trong các hằng số sau để chỉ định chế độ xảy ra làm tròn.

Hằng sốround[] examples

Using PHP_ROUND_HALF_UP with 1 decimal digit precision
float[1.6]
float[-1.6]

Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision
float[1.5]
float[-1.5]

Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision
float[1.6]
float[-1.6]

Using PHP_ROUND_HALF_ODD with 1 decimal digit precision
float[1.5]
float[-1.5]
1

Vòng num cách xa số 0 khi nó ở một nửa ở đó, tạo ra 1,5 thành 2 và -1,5 thành -2.

float[3]
float[4]
float[4]
float[4]
float[5.05]
float[5.06]
float[300]
float[0]
float[700]
float[1000]

Vòng num về phía 0 khi nó ở một nửa ở đó, biến 1,5 thành 1 và -1,5 thành -1.

Using PHP_ROUND_HALF_UP with 1 decimal digit precision
float[1.6]
float[-1.6]

Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision
float[1.5]
float[-1.5]

Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision
float[1.6]
float[-1.6]

Using PHP_ROUND_HALF_ODD with 1 decimal digit precision
float[1.5]
float[-1.5]
3

Vòng num cách xa số 0 khi nó ở một nửa ở đó, tạo ra 1,5 thành 2 và -1,5 thành -2.

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]

Vòng num về phía 0 khi nó ở một nửa ở đó, biến 1,5 thành 1 và -1,5 thành -1.

Using PHP_ROUND_HALF_UP with 1 decimal digit precision
float[1.6]
float[-1.6]

Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision
float[1.5]
float[-1.5]

Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision
float[1.6]
float[-1.6]

Using PHP_ROUND_HALF_ODD with 1 decimal digit precision
float[1.5]
float[-1.5]
5

Using PHP_ROUND_HALF_UP with 1 decimal digit precision
float[1.6]
float[-1.6]

Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision
float[1.5]
float[-1.5]

Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision
float[1.6]
float[-1.6]

Using PHP_ROUND_HALF_ODD with 1 decimal digit precision
float[1.5]
float[-1.5]
6

Using PHP_ROUND_HALF_UP with 1 decimal digit precision
float[1.6]
float[-1.6]

Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision
float[1.5]
float[-1.5]

Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision
float[1.6]
float[-1.6]

Using PHP_ROUND_HALF_ODD with 1 decimal digit precision
float[1.5]
float[-1.5]
7

Vòng num cách xa số 0 khi nó ở một nửa ở đó, tạo ra 1,5 thành 2 và -1,5 thành -2.

Rounding modes with 9.5
float[10]
float[9]
float[10]
float[9]

Rounding modes with 8.5
float[9]
float[8]
float[8]
float[9]

Vòng num về phía 0 khi nó ở một nửa ở đó, biến 1,5 thành 1 và -1,5 thành -1.

$num0

Using PHP_ROUND_HALF_UP with 1 decimal digit precision
float[1.6]
float[-1.6]

Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision
float[1.5]
float[-1.5]

Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision
float[1.6]
float[-1.6]

Using PHP_ROUND_HALF_ODD with 1 decimal digit precision
float[1.5]
float[-1.5]
6

$num2

Vòng num cách xa số 0 khi nó ở một nửa ở đó, tạo ra 1,5 thành 2 và -1,5 thành -2.

Using PHP_ROUND_HALF_UP with 1 decimal digit precision
float[1.6]
float[-1.6]

Using PHP_ROUND_HALF_DOWN with 1 decimal digit precision
float[1.5]
float[-1.5]

Using PHP_ROUND_HALF_EVEN with 1 decimal digit precision
float[1.6]
float[-1.6]

Using PHP_ROUND_HALF_ODD with 1 decimal digit precision
float[1.5]
float[-1.5]

Vòng num về phía 0 khi nó ở một nửa ở đó, biến 1,5 thành 1 và -1,5 thành -1.

  • Vòng num về phía giá trị thậm chí gần nhất khi nó ở đó một nửa, làm cho cả 1,5 và 2,5 thành 2.
  • Vòng num về phía giá trị lẻ gần nhất khi nó nằm ở đó một nửa, biến 1,5 thành 1 và 2,5 thành 3.
  • Trả về giá trị

Giá trị được làm tròn vào precision đã cho như một chiếc phao.

Thay đổi

$num3

$num4

$num5

$num6

$num7

$num8

Phiên bản

num không còn chấp nhận các đối tượng nội bộ hỗ trợ chuyển đổi số.

$num9

$precision0

$precision1

Ví dụ

Thay đổi

$precision2

$precision3

$precision1

Phiên bản

Thay đổi

$precision5

$precision6

$precision7

$precision1

Phiên bản

Thay đổi

$precision9

$mode0

$mode1

$mode2

$mode3

$mode4

$precision1

Phiên bản

num không còn chấp nhận các đối tượng nội bộ hỗ trợ chuyển đổi số.

$mode6

$mode7

$mode8

Ví dụ

Ví dụ: Vòng 1 vòng [] Ví dụ

Ví dụ trên sẽ xuất ra:

PHP_ROUND_HALF_UP0

Ví dụ #2 Làm thế nào precision ảnh hưởng đến một chiếc phao

Thay đổi

PHP_ROUND_HALF_UP1

PHP_ROUND_HALF_UP2

$precision1

Phiên bản

num không còn chấp nhận các đối tượng nội bộ hỗ trợ chuyển đổi số.

PHP_ROUND_HALF_UP4

PHP_ROUND_HALF_UP5

Ví dụ

Phiên bản

num không còn chấp nhận các đối tượng nội bộ hỗ trợ chuyển đổi số.

PHP_ROUND_HALF_UP7

PHP_ROUND_HALF_UP8

$precision1

Ví dụ

Ví dụ: Vòng 1 vòng [] Ví dụ

num0

num1

num2

$precision1

Ví dụ trên sẽ xuất ra:

Thay đổi

num4

num5

$precision1

Phiên bản

num không còn chấp nhận các đối tượng nội bộ hỗ trợ chuyển đổi số.

num7

num8

num9

precision0

precision1

$precision1

Ví dụ

Ví dụ: Vòng 1 vòng [] Ví dụ

precision3

precision4

$precision1

Ví dụ trên sẽ xuất ra:

Ví dụ #2 Làm thế nào precision ảnh hưởng đến một chiếc phao

precision6

precision7

precision8

precision9

Ví dụ #3

Rounding modes with 9.5
float[10]
float[9]
float[10]
float[9]

Rounding modes with 8.5
float[9]
float[8]
float[8]
float[9]
0 Ví dụ

Ví dụ #4

Rounding modes with 9.5
float[10]
float[9]
float[10]
float[9]

Rounding modes with 8.5
float[9]
float[8]
float[8]
float[9]
0 với precision ví dụ

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
00

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
01

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
02

$precision1

Xem thêm

trần [] - Phân số tròn lên

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
04

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
05

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
06

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
07

$precision1

Sàn [] - Phân số tròn xuống

Ví dụ: Vòng 1 vòng [] Ví dụ

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
09

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
10

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
11

$precision1

Ví dụ trên sẽ xuất ra:

Ví dụ #2 Làm thế nào precision ảnh hưởng đến một chiếc phao

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
13

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
14

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
15

$precision1

Ví dụ #3

Rounding modes with 9.5
float[10]
float[9]
float[10]
float[9]

Rounding modes with 8.5
float[9]
float[8]
float[8]
float[9]
0 Ví dụ

num không còn chấp nhận các đối tượng nội bộ hỗ trợ chuyển đổi số.

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
17

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
18

$precision1

Ví dụ

Ví dụ: Vòng 1 vòng [] Ví dụ

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
20

Ví dụ trên sẽ xuất ra:

Ví dụ: Vòng 1 vòng [] Ví dụ

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
21

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
22

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
23

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
24

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
25

$precision1

Ví dụ trên sẽ xuất ra:

Ví dụ #2 Làm thế nào precision ảnh hưởng đến một chiếc phao

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
27

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
28

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
29

$precision1

Ví dụ #3

Rounding modes with 9.5
float[10]
float[9]
float[10]
float[9]

Rounding modes with 8.5
float[9]
float[8]
float[8]
float[9]
0 Ví dụ

Ví dụ #4

Rounding modes with 9.5
float[10]
float[9]
float[10]
float[9]

Rounding modes with 8.5
float[9]
float[8]
float[8]
float[9]
0 với precision ví dụ

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
31

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
32

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
33

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
34

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
35

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
36

$precision1

Phiên bản

Ví dụ #2 Làm thế nào precision ảnh hưởng đến một chiếc phao

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
38

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
39

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
40

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
41

Ví dụ #3

Rounding modes with 9.5
float[10]
float[9]
float[10]
float[9]

Rounding modes with 8.5
float[9]
float[8]
float[8]
float[9]
0 Ví dụ

Thay đổi

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
42

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
43

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
44

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
45

$precision1

Phiên bản

num không còn chấp nhận các đối tượng nội bộ hỗ trợ chuyển đổi số.

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
47

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
48

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
49

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
50

$precision1

Phiên bản

num không còn chấp nhận các đối tượng nội bộ hỗ trợ chuyển đổi số.

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
52

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
53

$precision1

Ví dụ

num không còn chấp nhận các đối tượng nội bộ hỗ trợ chuyển đổi số.

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
55

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
56

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
57

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
58

$precision1

Ví dụ

Ví dụ: Vòng 1 vòng [] Ví dụ

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
60

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
61

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
62

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
63

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
64

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
65

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
66

$precision1

Ví dụ trên sẽ xuất ra:

Ví dụ #2 Làm thế nào precision ảnh hưởng đến một chiếc phao

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
68

float[135.79]
float[135.79]
float[135.8]
float[136]
float[140]
float[100]
float[0]
69

$precision1

Làm thế nào để tôi làm tròn đến 2 số thập phân trong PHP?

Ví dụ số 1 vòng [] Ví dụ Php Echo Round [3.4]; // 3 Echo Round [3.5]; // 4 Echo Round [3.6]; // 4 vòng lặp [3.6, 0]; // 4 vòng Echo [1.95583, 2]; // 1,96 vòng Echo [1241757, -3]; // 1242000 Echo Round [5.045, 2]; // 5,05 Vòng Echo [5.055, 2]; // 5.06?>round[] examples php echo round[3.4]; // 3 echo round[3.5]; // 4 echo round[3.6]; // 4 echo round[3.6, 0]; // 4 echo round[1.95583, 2]; // 1.96 echo round[1241757, -3]; // 1242000 echo round[5.045, 2]; // 5.05 echo round[5.055, 2]; // 5.06 ?>

Bạn có thể làm tròn phao?

Vòng [] vòng [] là một hàm tích hợp có sẵn với Python.Nó sẽ trả lại cho bạn một số float sẽ được làm tròn đến các vị trí thập phân được đưa ra làm đầu vào.Nếu các vị trí thập phân được làm tròn không được chỉ định, nó được coi là 0 và nó sẽ tròn đến số nguyên gần nhất.Round[] is a built-in function available with python. It will return you a float number that will be rounded to the decimal places which are given as input. If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer.

Làm thế nào để bạn làm tròn một vị trí thập phân trong PHP?

Echo 'Sử dụng PHP_ROUND_HALF_DOWN với độ chính xác 1 chữ số thập phân'.PHP_EOL;var_dump [vòng [1,55, 1, php_round_half_down]];var_dump [vòng [-1,55, 1, PHP_ROUND_HALF_DOWN]];var_dump[round[-1.55, 1, PHP_ROUND_HALF_DOWN]];

Làm thế nào để bạn làm tròn đến tổng số gần nhất trong PHP?

Hàm CLE [] là một hàm tích hợp trong PHP và được sử dụng để làm tròn một số vào số nguyên lớn hơn gần nhất.Tham số: Hàm CARE [] chấp nhận một giá trị tham số duy nhất đại diện cho số bạn muốn làm tròn lên đến số nguyên lớn hơn gần nhất.ceil[] function is a built-in function in PHP and is used to round a number to the nearest greater integer. Parameters: The ceil[] function accepts a single parameter value which represents the number which you want to round up to the nearest greater integer.

Bài Viết Liên Quan

Chủ Đề