Hướng dẫn does python int round down or up? - python int làm tròn xuống hay lên?

Khi chương trình Python của chúng tôi hoạt động với các giá trị số, thường thì chúng tôi có các giá trị lượt với thành phần phân số thành các số toàn bộ (số nguyên). Nhưng làm thế nào để làm như vậy? Và Python có tùy chọn nào? Hãy cùng nhau tìm hiểu.

Show

Trong bài viết này:

  • Vòng giá trị số lên xuống trong Python
  • Giá trị tròn lên xuống: Chức năng Python từ
    # Some numbers to round
    valueA = 3.14159265359
    valueB = 1845.7409947
    valueC = -100.95
    valueD = 9.5432
    valueE = 34.49953
    
    # Round values to whole numbers
    roundA = round(valueA)
    roundB = round(valueB)
    roundC = round(valueC)
    roundD = round(valueD)
    roundE = round(valueE)
    
    # Output rounded values
    print("Value:".ljust(15), "Rounded:")
    
    print(str(valueA).ljust(15), roundA)
    print(str(valueB).ljust(15), roundB)
    print(str(valueC).ljust(15), roundC)
    print(str(valueD).ljust(15), roundD)
    print(str(valueE).ljust(15), roundE)
    
    5
    • Ví dụ: Số python tròn đến số nguyên đầy đủ gần nhất
  • Làm tròn xuống số nguyên tiếp theo: Chức năng Python từ
    # Some numbers to round
    valueA = 3.14159265359
    valueB = 1845.7409947
    valueC = -100.95
    valueD = 9.5432
    valueE = 34.49953
    
    # Round values to whole numbers
    roundA = round(valueA)
    roundB = round(valueB)
    roundC = round(valueC)
    roundD = round(valueD)
    roundE = round(valueE)
    
    # Output rounded values
    print("Value:".ljust(15), "Rounded:")
    
    print(str(valueA).ljust(15), roundA)
    print(str(valueB).ljust(15), roundB)
    print(str(valueC).ljust(15), roundC)
    print(str(valueD).ljust(15), roundD)
    print(str(valueE).ljust(15), roundE)
    
    6
    • Ví dụ: Các giá trị tròn xuống số nguyên đầy đủ tiếp theo
  • Làm tròn đến số nguyên tiếp theo: Chức năng Python từ
    # Some numbers to round
    valueA = 3.14159265359
    valueB = 1845.7409947
    valueC = -100.95
    valueD = 9.5432
    valueE = 34.49953
    
    # Round values to whole numbers
    roundA = round(valueA)
    roundB = round(valueB)
    roundC = round(valueC)
    roundD = round(valueD)
    roundE = round(valueE)
    
    # Output rounded values
    print("Value:".ljust(15), "Rounded:")
    
    print(str(valueA).ljust(15), roundA)
    print(str(valueB).ljust(15), roundB)
    print(str(valueC).ljust(15), roundC)
    print(str(valueD).ljust(15), roundD)
    print(str(valueE).ljust(15), roundE)
    
    7
    • Ví dụ: Giá trị Python tròn lên đến toàn bộ số
  • Làm tròn tất cả các giá trị trong danh sách hoặc mảng python
    • Giá trị python tròn với danh sách hiểu
    • Làm tròn tất cả các giá trị với vòng lặp Python từ
      # Some numbers to round
      valueA = 3.14159265359
      valueB = 1845.7409947
      valueC = -100.95
      valueD = 9.5432
      valueE = 34.49953
      
      # Round values to whole numbers
      roundA = round(valueA)
      roundB = round(valueB)
      roundC = round(valueC)
      roundD = round(valueD)
      roundE = round(valueE)
      
      # Output rounded values
      print("Value:".ljust(15), "Rounded:")
      
      print(str(valueA).ljust(15), roundA)
      print(str(valueB).ljust(15), roundB)
      print(str(valueC).ljust(15), roundC)
      print(str(valueD).ljust(15), roundD)
      print(str(valueE).ljust(15), roundE)
      
      8
  • Bản tóm tắt

# Vòng giá trị số lên xuống trong Python

Khi chúng ta làm tròn các giá trị, chúng ta đi từ một giá trị số với các vị trí thập phân đến một số toàn bộ. Với quá trình này, chúng tôi mất một số độ chính xác, nhưng giá trị tròn thường dễ đọc và giải thích hơn nhiều.

Python có ba cách để biến giá trị dấu phẩy động thành số toàn bộ (số nguyên):

  • Hàm
    # Some numbers to round
    valueA = 3.14159265359
    valueB = 1845.7409947
    valueC = -100.95
    valueD = 9.5432
    valueE = 34.49953
    
    # Round values to whole numbers
    roundA = round(valueA)
    roundB = round(valueB)
    roundC = round(valueC)
    roundD = round(valueD)
    roundE = round(valueE)
    
    # Output rounded values
    print("Value:".ljust(15), "Rounded:")
    
    print(str(valueA).ljust(15), roundA)
    print(str(valueB).ljust(15), roundB)
    print(str(valueC).ljust(15), roundC)
    print(str(valueD).ljust(15), roundD)
    print(str(valueE).ljust(15), roundE)
    
    5 tích hợp làm tròn các giá trị lên xuống.
  • Hàm
    # Some numbers to round
    valueA = 3.14159265359
    valueB = 1845.7409947
    valueC = -100.95
    valueD = 9.5432
    valueE = 34.49953
    
    # Round values to whole numbers
    roundA = round(valueA)
    roundB = round(valueB)
    roundC = round(valueC)
    roundD = round(valueD)
    roundE = round(valueE)
    
    # Output rounded values
    print("Value:".ljust(15), "Rounded:")
    
    print(str(valueA).ljust(15), roundA)
    print(str(valueB).ljust(15), roundB)
    print(str(valueC).ljust(15), roundC)
    print(str(valueD).ljust(15), roundD)
    print(str(valueE).ljust(15), roundE)
    
    6 làm tròn đến số nguyên đầy đủ tiếp theo.
  • Hàm
    # Some numbers to round
    valueA = 3.14159265359
    valueB = 1845.7409947
    valueC = -100.95
    valueD = 9.5432
    valueE = 34.49953
    
    # Round values to whole numbers
    roundA = round(valueA)
    roundB = round(valueB)
    roundC = round(valueC)
    roundD = round(valueD)
    roundE = round(valueE)
    
    # Output rounded values
    print("Value:".ljust(15), "Rounded:")
    
    print(str(valueA).ljust(15), roundA)
    print(str(valueB).ljust(15), roundB)
    print(str(valueC).ljust(15), roundC)
    print(str(valueD).ljust(15), roundD)
    print(str(valueE).ljust(15), roundE)
    
    7 làm tròn đến số nguyên đầy đủ tiếp theo.

Nếu bạn chỉ muốn một chuỗi hoặc đầu ra tập lệnh với toàn bộ số, thì chuỗi định dạng Python cũng có thể thực hiện tác vụ đó. Bằng cách đó, bạn cũng không mất độ chính xác trong giá trị ban đầu.

Hãy cùng xem ba cách tiếp cận này hoạt động như thế nào.

# Giá trị tròn lên và xuống: Chức năng Python từ # Some numbers to round valueA = 3.14159265359 valueB = 1845.7409947 valueC = -100.95 valueD = 9.5432 valueE = 34.49953 # Round values to whole numbers roundA = round(valueA) roundB = round(valueB) roundC = round(valueC) roundD = round(valueD) roundE = round(valueE) # Output rounded values print("Value:".ljust(15), "Rounded:") print(str(valueA).ljust(15), roundA) print(str(valueB).ljust(15), roundB) print(str(valueC).ljust(15), roundC) print(str(valueD).ljust(15), roundD) print(str(valueE).ljust(15), roundE) 5

Để làm tròn các giá trị điểm nổi lên xuống, chúng tôi sử dụng chức năng Python từ

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
5 (Lutz, 2013; Python Docs, N.D. A). Có hai cách để sử dụng chức năng này. Tùy chọn đầu tiên là làm tròn các giá trị đến một số thập phân nhất định. Tùy chọn khác biến giá trị dấu phẩy động thành một số toàn bộ.

Để làm điều đó sau, chúng tôi gọi

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
5 với một đối số: giá trị để biến thành một số nguyên. Ví dụ:

round(5.4598)
# Returns: 5

Hàm

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
5 làm tròn các giá trị của
Value:          Rounded:
3.14159265359   3
1845.7409947    1846
-100.95         -101
9.55432         10
34.49953        34
6 đối với số nguyên chẵn (Python Docs, n.d. A). Vì vậy,
Value:          Rounded:
3.14159265359   3
1845.7409947    1846
-100.95         -101
9.55432         10
34.49953        34
6 được làm tròn cho các giá trị dương và làm tròn cho các giá trị âm.

Chẳng hạn, cả

Value:          Rounded:
3.14159265359   3
1845.7409947    1846
-100.95         -101
9.55432         10
34.49953        34
8 và
Value:          Rounded:
3.14159265359   3
1845.7409947    1846
-100.95         -101
9.55432         10
34.49953        34
9 trả về
import math

math.floor(12.75)
# Returns: 12
0, trong khi
import math

math.floor(12.75)
# Returns: 12
1 cho
import math

math.floor(12.75)
# Returns: 12
2 và
import math

math.floor(12.75)
# Returns: 12
3 cho
import math

math.floor(12.75)
# Returns: 12
4. Hành vi Python này hơi khác so với cách làm tròn thường đi.

Khi chúng ta cung cấp cho

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
5 một số nguyên, hàm chỉ đơn giản là trả về toàn bộ số đó. Không có lỗi nào trong trường hợp đó, và vì vậy chúng tôi không phải kiểm tra xem đối số chức năng có giá trị phân số không. Nó cần phải là một con số mặc dù; Giá trị chuỗi không được phép trong
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
5.

# Ví dụ: Số python tròn đến số nguyên đầy đủ gần nhất

Để xem chức năng

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
5 hoạt động như thế nào trong thực tế, hãy để xem xét các chương trình nhỏ sau:

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)

Ở đây trước tiên chúng tôi tạo năm biến với các giá trị dấu phẩy động. Một số có rất nhiều vị trí thập phân và một số khác chỉ một vài.

Sau đó, chúng tôi làm một số làm tròn. Vì vậy, chúng tôi gọi hàm

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
5 và cung cấp một đối số: giá trị thành vòng. Chúng tôi lưu trữ các số nguyên tròn trong các biến
import math

math.floor(12.75)
# Returns: 12
9 đến
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
0.

Tiếp theo, chúng tôi đưa ra kết quả với hàm

import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
1. Đối với mỗi biến, chúng tôi hiển thị giá trị ban đầu (ví dụ:
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
2) và kết quả làm tròn của nó (
import math

math.floor(12.75)
# Returns: 12
9). Với phương thức chuỗi
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
4, chúng tôi biện minh cho giá trị đầu tiên ở bên trái. Điều đó phù hợp với các giá trị cho đầu ra đẹp hơn.

Ở đây, cách các giá trị tròn trông như thế nào:

Value:          Rounded:
3.14159265359   3
1845.7409947    1846
-100.95         -101
9.55432         10
34.49953        34

# Làm tròn xuống số nguyên tiếp theo: Chức năng Python từ # Some numbers to round valueA = 3.14159265359 valueB = 1845.7409947 valueC = -100.95 valueD = 9.5432 valueE = 34.49953 # Round values to whole numbers roundA = round(valueA) roundB = round(valueB) roundC = round(valueC) roundD = round(valueD) roundE = round(valueE) # Output rounded values print("Value:".ljust(15), "Rounded:") print(str(valueA).ljust(15), roundA) print(str(valueB).ljust(15), roundB) print(str(valueC).ljust(15), roundC) print(str(valueD).ljust(15), roundD) print(str(valueE).ljust(15), roundE) 6

Hàm

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6 trả về giá trị sàn của đối số của nó, đó là số nguyên gần nhất ít hơn hoặc bằng với giá trị đối số đó (Python Docs, n.d. b).

Điều đó nghe có vẻ trừu tượng, nhưng chỉ là một cách khác để nói rằng

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6 làm tròn đến tổng số tiếp theo. Vì vậy,
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
8 trở thành
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
9 và
11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
0 được biến thành
11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
1. Và vì hàm làm tròn xuống một giá trị nhỏ hơn,
11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
2 trở thành
11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
3.

Dưới đây, một ví dụ nhanh về chức năng

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6:

import math

math.floor(12.75)
# Returns: 12

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6 chỉ chấp nhận một đối số: giá trị để làm tròn xuống. Với một chức năng tùy chỉnh nhỏ, chúng tôi cũng có thể làm tròn xuống một số vị trí thập phân. Xem tròn xuống một số thập phân cụ thể để biết thêm.

# Ví dụ: Các giá trị tròn xuống số nguyên đầy đủ tiếp theo

Để khám phá cách thức hoạt động của hàm

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6, hãy để kiểm tra chương trình Python sau:

import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)

Trước tiên chúng tôi nhập mô -đun

11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
7. Điều đó làm cho nó có thể sử dụng chức năng
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6. Sau đó, chúng tôi thực hiện năm biến,
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
2 đến
import math

math.ceil(12.45)
# Returns: 13
0. Mỗi cái giữ một giá trị dấu phẩy động.

Tiếp theo chúng tôi làm tròn những giá trị đó xuống. Vì vậy, chúng tôi gọi hàm

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6 trên mỗi biến. Chúng tôi lưu trữ kết quả trong các biến mới (
import math

math.floor(12.75)
# Returns: 12
9 đến
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
0).

Phần thứ ba của chương trình xuất hiện các biến với chức năng Python từ

import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
1. Ở đây mỗi câu lệnh
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
1 hiển thị giá trị ban đầu và phiên bản làm tròn của nó. Đây là cách trông như thế nào:

11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10

# Làm tròn đến số nguyên tiếp theo: Hàm Python từ # Some numbers to round valueA = 3.14159265359 valueB = 1845.7409947 valueC = -100.95 valueD = 9.5432 valueE = 34.49953 # Round values to whole numbers roundA = round(valueA) roundB = round(valueB) roundC = round(valueC) roundD = round(valueD) roundE = round(valueE) # Output rounded values print("Value:".ljust(15), "Rounded:") print(str(valueA).ljust(15), roundA) print(str(valueB).ljust(15), roundB) print(str(valueC).ljust(15), roundC) print(str(valueD).ljust(15), roundD) print(str(valueE).ljust(15), roundE) 7

Hàm

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 trả về trần của đối số của nó, đó là số nguyên gần nhất lớn hơn hoặc bằng với giá trị đối số đó (Python Docs, n.d. b).

Đó chỉ là một cách để nói rằng

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 làm tròn lên một số toàn bộ:
import math

math.ceil(12.45)
# Returns: 13
9 trở thành
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values up to the nearest full integer
roundA = math.ceil(valueA)
roundB = math.ceil(valueB)
roundC = math.ceil(valueC)
roundD = math.ceil(valueD)
roundE = math.ceil(valueE)

# Output the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
0 và
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values up to the nearest full integer
roundA = math.ceil(valueA)
roundB = math.ceil(valueB)
roundC = math.ceil(valueC)
roundD = math.ceil(valueD)
roundE = math.ceil(valueE)

# Output the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
1 bị biến thành
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values up to the nearest full integer
roundA = math.ceil(valueA)
roundB = math.ceil(valueB)
roundC = math.ceil(valueC)
roundD = math.ceil(valueD)
roundE = math.ceil(valueE)

# Output the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
2. Và bởi vì hàm làm tròn lên đến một giá trị lớn hơn,
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values up to the nearest full integer
roundA = math.ceil(valueA)
roundB = math.ceil(valueB)
roundC = math.ceil(valueC)
roundD = math.ceil(valueD)
roundE = math.ceil(valueE)

# Output the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
3 trở thành
11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
3.

Ở đây, một ví dụ nhanh chóng về

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7:

import math

math.ceil(12.45)
# Returns: 13

Ở đây, một cách để ghi nhớ sự khác biệt giữa

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6 và
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7. Như bạn đã biết, mỗi giá trị điểm nổi nằm giữa hai số nguyên liên tiếp.
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values up to the nearest full integer
roundA = math.ceil(valueA)
roundB = math.ceil(valueB)
roundC = math.ceil(valueC)
roundD = math.ceil(valueD)
roundE = math.ceil(valueE)

# Output the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
8, ví dụ, nằm trong khoảng từ
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values up to the nearest full integer
roundA = math.ceil(valueA)
roundB = math.ceil(valueB)
roundC = math.ceil(valueC)
roundD = math.ceil(valueD)
roundE = math.ceil(valueE)

# Output the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
9 và
11.282985 rounded = 12
19.2545879 rounded = 20
0.50000001 rounded = 1
34.6403001 rounded = 35
-9.9121138 rounded = -9
0.

Bây giờ trần trần của người Viking là điểm cuối cao hơn của khoảng này. Vì vậy,

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 trả về
11.282985 rounded = 12
19.2545879 rounded = 20
0.50000001 rounded = 1
34.6403001 rounded = 35
-9.9121138 rounded = -9
0. Điểm bắt đầu thấp hơn của khoảng số nguyên đó được gọi là sàn nhà. Vì vậy,
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6 trả về 12.

Chức năng Python từ

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 luôn làm tròn lên đến một số nguyên đầy đủ. Nhưng với một chức năng tùy chỉnh nhỏ, chúng ta cũng có thể làm tròn đến một số vị trí thập phân. Xem các giá trị Python tròn đến các vị trí thập phân cho cách làm thế nào.

# Ví dụ: Giá trị Python tròn lên đến toàn bộ số

Hãy cùng xem cách

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 hoạt động trong thực tế. Chương trình ví dụ này làm tròn một số giá trị dấu phẩy động lên đến một số toàn bộ:

import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values up to the nearest full integer
roundA = math.ceil(valueA)
roundB = math.ceil(valueB)
roundC = math.ceil(valueC)
roundD = math.ceil(valueD)
roundE = math.ceil(valueE)

# Output the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)

Trước tiên chúng tôi nhập mô -đun

11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
7. Điều đó làm cho chức năng
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 có sẵn. Sau đó, chúng tôi thực hiện năm biến khác nhau, được đặt tên là
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
2 đến
import math

math.ceil(12.45)
# Returns: 13
0. Mỗi người có một số với một số vị trí thập phân.

Phần tiếp theo làm tròn các giá trị đó lên đến một số nguyên đầy đủ. Để thực hiện điều đó, chúng tôi gọi hàm

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 trên mỗi biến. Chúng tôi đặt các giá trị trả về chức năng đó trong các biến mới,
import math

math.floor(12.75)
# Returns: 12
9 đến
import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
0.

Phân đoạn mã thứ ba có hàm

import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
1 đầu ra cả giá trị gốc và tròn. Ở đây, những gì hiển thị:

11.282985 rounded = 12
19.2545879 rounded = 20
0.50000001 rounded = 1
34.6403001 rounded = 35
-9.9121138 rounded = -9

# Làm tròn tất cả các giá trị trong danh sách hoặc mảng python

Tất nhiên cũng có những tình huống chúng tôi có một loạt các giá trị để làm tròn, thay vì một giá trị duy nhất. Có hai cách chính để làm điều đó: với một danh sách hiểu hoặc vòng lặp

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
8. Hãy xem nào.

# Vòng python giá trị với danh sách hiểu biết

Khi chúng ta có một chuỗi các giá trị dấu phẩy động, một cách để làm tròn chúng là với sự hiểu biết danh sách. Điều đó chỉ cần một chút mã và chạy hiệu quả.

Ở đây, một chương trình nhỏ làm điều đó:

import math

# Some random values
values = [
    3.46410162, 6.70820393, 11.04536102,
    15.29705854, 21.21320344, 31.4960315
]

# Generate new lists with values rounded
valuesRounded = [round(number) for number in values]
valuesRoundUp = [math.ceil(number) for number in values]
valuesRoundDown = [math.floor(number) for number in values]

# Output data
print("Original values:\n", values)
print("Rounded:\n", valuesRounded)
print("Rounded up to next integer:\n", valuesRoundUp)
print("Rounded down to next integer:\n", valuesRoundDown)

Đầu tiên chúng tôi nhập mô -đun

11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
7. Điều đó làm cho các chức năng làm tròn
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 và
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6 có sẵn. Sau đó, chúng tôi lập một danh sách có tên
import math

# Some random values
values = [
    3.46410162, 6.70820393, 11.04536102,
    15.29705854, 21.21320344, 31.4960315
]

# Generate new lists with values rounded
valuesRounded = [round(number) for number in values]
valuesRoundUp = [math.ceil(number) for number in values]
valuesRoundDown = [math.floor(number) for number in values]

# Output data
print("Original values:\n", values)
print("Rounded:\n", valuesRounded)
print("Rounded up to next integer:\n", valuesRoundUp)
print("Rounded down to next integer:\n", valuesRoundDown)
8, chứa một số giá trị dấu phẩy động.

Để làm tròn các giá trị đó vào toàn bộ số, chúng tôi thực hiện ba toàn bộ danh sách. Cái đầu tiên thực hiện

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
5 cho mỗi giá trị danh sách. Hai chức năng khác thực thi
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 và
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6 trên các giá trị danh sách.

Chúng tôi tạo ra các giá trị mà các hàm đó sử dụng với vòng lặp

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
8:
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
03. Điều này lấy một giá trị từ danh sách
import math

# Some random values
values = [
    3.46410162, 6.70820393, 11.04536102,
    15.29705854, 21.21320344, 31.4960315
]

# Generate new lists with values rounded
valuesRounded = [round(number) for number in values]
valuesRoundUp = [math.ceil(number) for number in values]
valuesRoundDown = [math.floor(number) for number in values]

# Output data
print("Original values:\n", values)
print("Rounded:\n", valuesRounded)
print("Rounded up to next integer:\n", valuesRoundUp)
print("Rounded down to next integer:\n", valuesRoundDown)
8 tại một thời điểm và cung cấp nó thông qua biến
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
05.

Những toàn bộ danh sách đó tạo ra danh sách mới. Chúng tôi gán các biến đó cho các biến

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
06,
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
07 và
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
08.

Phần cuối cùng của chương trình đưa ra danh sách ban đầu và ba phần tròn. Đây là cách trông như thế nào:

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
0

Trong ví dụ trên, chúng tôi giữ danh sách ban đầu. Nếu bạn không cần phải giữ lại các giá trị đó, bạn cũng có thể ghi đè danh sách ban đầu với các giá trị tròn. Đây là cách mà một danh sách hiểu được điều đó:

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
1

# Làm tròn tất cả các giá trị với vòng lặp Python # Some numbers to round valueA = 3.14159265359 valueB = 1845.7409947 valueC = -100.95 valueD = 9.5432 valueE = 34.49953 # Round values to whole numbers roundA = round(valueA) roundB = round(valueB) roundC = round(valueC) roundD = round(valueD) roundE = round(valueE) # Output rounded values print("Value:".ljust(15), "Rounded:") print(str(valueA).ljust(15), roundA) print(str(valueB).ljust(15), roundB) print(str(valueC).ljust(15), roundC) print(str(valueD).ljust(15), roundD) print(str(valueE).ljust(15), roundE) 8

Tất nhiên chúng ta cũng có thể làm tròn các giá trị danh sách hoặc mảng với vòng lặp

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
8 thông thường. Điều này đòi hỏi nhiều mã hơn một chút so với khả năng hiểu danh sách, nhưng giúp dễ dàng thực hiện các hoạt động bổ sung trên mỗi yếu tố. Cộng với một vòng
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
8 dễ đọc hơn trong các tình huống phức tạp.

Tại đây, cách một chương trình Python làm tròn các giá trị bên trong vòng lặp

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
8:

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
2

Trước tiên chúng tôi nhập mô -đun

11.282985 rounded = 11
19.2545879 rounded = 19
0.50000001 rounded = 0
34.6403001 rounded = 34
-9.9121138 rounded = -10
7 để có thể sử dụng
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 và
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6. Sau đó, chúng tôi lập một danh sách (
import math

# Some random values
values = [
    3.46410162, 6.70820393, 11.04536102,
    15.29705854, 21.21320344, 31.4960315
]

# Generate new lists with values rounded
valuesRounded = [round(number) for number in values]
valuesRoundUp = [math.ceil(number) for number in values]
valuesRoundDown = [math.floor(number) for number in values]

# Output data
print("Original values:\n", values)
print("Rounded:\n", valuesRounded)
print("Rounded up to next integer:\n", valuesRoundUp)
print("Rounded down to next integer:\n", valuesRoundDown)
8) với các giá trị dấu phẩy động.

Sau đó, chúng tôi thực hiện ba danh sách trống ban đầu:

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
06,
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
07 và
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
08. Chúng sẽ giữ các giá trị tròn của chúng tôi.

Để lấp đầy các danh sách đó, chúng tôi tạo một vòng lặp Python

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
8. Vòng lặp này đi qua tất cả các yếu tố trong danh sách
import math

# Some random values
values = [
    3.46410162, 6.70820393, 11.04536102,
    15.29705854, 21.21320344, 31.4960315
]

# Generate new lists with values rounded
valuesRounded = [round(number) for number in values]
valuesRoundUp = [math.ceil(number) for number in values]
valuesRoundDown = [math.floor(number) for number in values]

# Output data
print("Original values:\n", values)
print("Rounded:\n", valuesRounded)
print("Rounded up to next integer:\n", valuesRoundUp)
print("Rounded down to next integer:\n", valuesRoundDown)
8. Trong mỗi chu kỳ vòng lặp, biến
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
05 giữ một phần tử duy nhất từ ​​danh sách đó.

Bên trong vòng lặp, chúng tôi gọi phương thức

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
23 trên mỗi ba danh sách mới. Bằng cách đó, chúng tôi thêm một yếu tố mới cho họ. Giá trị chúng tôi thêm mỗi lần vượt qua vòng lặp là biến
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
05 được làm tròn với
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
5,
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 và
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6. Sau khi vòng lặp này được thực hiện, mỗi danh sách trong ba danh sách đó có một giá trị tròn từ danh sách ban đầu.

Chương trình kết thúc với một số câu

import math

# Some random values
valueA = 11.2829850
valueB = 19.2545879
valueC = 0.50000001
valueD = 34.6403001
valueE = -9.9121138

# Round values down to the nearest full integer
roundA = math.floor(valueA)
roundB = math.floor(valueB)
roundC = math.floor(valueC)
roundD = math.floor(valueD)
roundE = math.floor(valueE)

# Print the results
print(valueA, "rounded =", roundA)
print(valueB, "rounded =", roundB)
print(valueC, "rounded =", roundC)
print(valueD, "rounded =", roundD)
print(valueE, "rounded =", roundE)
1. Hiển thị danh sách ban đầu và các dẫn xuất tròn của nó. Ở đây, đầu ra đó trông như thế nào:

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
3

Nhân tiện, bạn không phải lập một danh sách mới khi bạn làm tròn các giá trị. Nếu bạn tốt với việc mất dữ liệu gốc, bạn cũng có thể ghi đè danh sách hiện có. Làm như vậy thật dễ dàng khi bạn kết hợp vòng lặp

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
8 với hàm
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
30:

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
4

ĐỌC THÊM

  • Giá trị trăn tròn đến một số vị trí thập phân nhất định
  • Cắt ngắn các giá trị điểm nổi python thành một số
  • Cắt ngắn giá trị python thành một số chữ số thập phân nhất định

# Bản tóm tắt

Python có ba cách để làm tròn giá trị dấu phẩy động đến một số toàn bộ. Hàm

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
5 làm tròn giá trị lên hoặc xuống. Một chữ số thập phân của
Value:          Rounded:
3.14159265359   3
1845.7409947    1846
-100.95         -101
9.55432         10
34.49953        34
6 có vòng python hướng tới một số nguyên thậm chí. Điều đó làm cho nó làm tròn cho các giá trị tích cực và xuống cho những giá trị tiêu cực.

Hàm

# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
6, mặt khác, luôn làm tròn xuống số nguyên đầy đủ gần nhất.
# Some numbers to round
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round values to whole numbers
roundA = round(valueA)
roundB = round(valueB)
roundC = round(valueC)
roundD = round(valueD)
roundE = round(valueE)

# Output rounded values
print("Value:".ljust(15), "Rounded:")

print(str(valueA).ljust(15), roundA)
print(str(valueB).ljust(15), roundB)
print(str(valueC).ljust(15), roundC)
print(str(valueD).ljust(15), roundD)
print(str(valueE).ljust(15), roundE)
7 làm ngược lại. Chức năng đó luôn làm tròn lên đến một số toàn bộ.

Tất cả ba chức năng hoạt động theo cùng một cách: cung cấp chức năng với một đối số, đó là giá trị điểm nổi thành vòng.

Xuất bản ngày 20 tháng 12 năm 2019.

«Tất cả các bài báo Python

Python int () có tròn không?

Int tròn về phía 0: Đối với x dương tính, nó giống như sàn, đối với x âm, nó giống như trần nhà. Vòng tròn đến giải pháp gần nhất có thể: Vòng (1.49) = 1 và Vòng (1.51) == 2. Khi x chính xác giữa hai số, vòng (x) sẽ là số chẵn gần nhất.: For positive x it is like floor , for negative x it is like ceil . round rounds to the closest possible solution: round(1.49)=1 and round(1.51)==2 . When x is precisely between two numbers, round(x) will be the closest even number.

INT làm tròn lên hay xuống?

Int làm tròn xuống số nguyên gần nhất.Cắt ngắn số lượng chỉ vào phần số nguyên bằng cách loại bỏ bất kỳ phần thập phân nào.. Trunc truncates the number to just the integer portion by removing any decimal portion.

0,5 làm tròn lên hay xuống Python?

Đối với 0,5, nó làm tròn lên.For = 0,5, hàm vòng () làm tròn số đến số chẵn gần nhất.Vì vậy, 0,5 được làm tròn về 0, và -0,5;33,5 và 34,5 đều được làm tròn đến 34;-33,5 -34,5 đều được làm tròn đến -34, v.v.. For =0.5, the round() function rounds the number off to the nearest even number. So, 0.5 is rounded to zero, and so is -0.5; 33.5 and 34.5 are both rounded off to 34; -33.5 -34.5 are both rounded off to -34, and so on.

Python có tròn luôn tròn không?

Ví dụ, 9,8 làm tròn đến 10, chỉ có chênh lệch 0,2.Tương tự như vậy, 10,2 làm tròn đến 10, với sự khác biệt tương tự.Vì vậy, mục tiêu duy nhất là có được một giá trị gần với giá trị ban đầu nhưng ở dạng đơn giản hơn.Hãy đi sâu hơn vào vòng tròn ở Python!... Ví dụ -.