Hướng dẫn how do you control decimal places in python? - làm thế nào để bạn kiểm soát vị trí thập phân trong python?

Tldr;)

Vấn đề làm tròn của đầu vào và đầu ra đã được giải quyết dứt khoát bằng Python 3.1 và bản sửa lỗi cũng được đưa vào Python 2.7.0.solved definitively by Python 3.1 and the fix is backported also to Python 2.7.0.

Show

Các số tròn có thể được chuyển đổi đảo ngược giữa phao và chuỗi qua lại:

import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.
8 hoặc
import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.
9
back and forth:
import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.
8 or
import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.
9

>>> 0.3
0.3
>>> float(repr(0.3)) == 0.3
True

Một loại

round(12.54673, 2)
# Returns: 12.55
0 không cần thiết để lưu trữ nữa.

Kết quả của các hoạt động số học phải được làm tròn một lần nữa vì các lỗi làm tròn có thể tích lũy không chính xác hơn so với điều đó có thể xảy ra sau khi phân tích một số. Không được cố định bởi thuật toán

round(12.54673, 2)
# Returns: 12.55
1 được cải thiện (Python> = 3.1,> = 2.7.0): because rounding errors could accumulate more inaccuracy than that is possible after parsing one number. That is not fixed by the improved
round(12.54673, 2)
# Returns: 12.55
1 algorithm (Python >= 3.1, >= 2.7.0):

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)

The output string function

round(12.54673, 2)
# Returns: 12.55
2 was rounded to 12 valid digits in Python < 2.7x and < 3.1, to prevent excessive invalid digits similar to unfixed repr() output. That was still insufficientl after subtraction of very similar numbers and it was too much rounded after other operations. Python 2.7 and 3.1 use the same length of str() although the repr() is fixed. Some old versions of Numpy had also excessive invalid digits, even with fixed Python. The current Numpy is fixed. Python versions >= 3.2 have the same results of str() and repr() function and also output of similar functions in Numpy.


Bài kiểm tra

import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.

Tài liệu

Xem Ghi chú phát hành Python 2.7 - Ngôn ngữ khác thay đổi đoạn thứ tư:

Chuyển đổi giữa các số dấu phẩy động và chuỗi hiện được làm tròn chính xác trên hầu hết các nền tảng. Những chuyển đổi này xảy ra ở nhiều nơi khác nhau: str () trên phao và số phức; các nhà xây dựng nổi và phức tạp; định dạng số; tuần tự hóa và khử tự động các phao và số phức bằng các mô-đun

round(12.54673, 2)
# Returns: 12.55
3,
round(12.54673, 2)
# Returns: 12.55
4 và
round(12.54673, 2)
# Returns: 12.55
5; phân tích cú pháp phao và văn học tưởng tượng trong mã Python; và chuyển đổi thập phân sang phao.
between floating-point numbers and strings are now correctly rounded on most platforms. These conversions occur in many different places: str() on floats and complex numbers; the float and complex constructors; numeric formatting; serializing and de-serializing floats and complex numbers using the
round(12.54673, 2)
# Returns: 12.55
3,
round(12.54673, 2)
# Returns: 12.55
4 and
round(12.54673, 2)
# Returns: 12.55
5 modules; parsing of float and imaginary literals in Python code; and Decimal-to-float conversion.

Liên quan đến điều này, phần repr () của số điểm nổi x hiện đang trả về kết quả dựa trên chuỗi thập phân ngắn nhất mà Lọ đảm bảo quay trở lại X dưới cách làm tròn chính xác (với chế độ làm tròn nửa vòng tròn). Trước đây, nó đã đưa ra một chuỗi dựa trên làm tròn x đến 17 chữ số thập phân.repr() of a floating-point number x now returns a result based on the shortest decimal string that’s guaranteed to round back to x under correct rounding (with round-half-to-even rounding mode). Previously it gave a string based on rounding x to 17 decimal digits.

Vấn đề liên quan


Thông tin thêm: Định dạng

round(12.54673, 2)
# Returns: 12.55
6 trước Python 2.7 tương tự như
round(12.54673, 2)
# Returns: 12.55
7 hiện tại. Cả hai loại đều sử dụng cùng độ chính xác gấp đôi của IEEE 754 với 52 bit mantissa. Một sự khác biệt lớn là
round(12.54673, 2)
# Returns: 12.55
8 được định dạng thường xuyên với số thập phân quá mức để không thể mất một chút, nhưng không có số IEEE 754 hợp lệ tồn tại trong khoảng 13.94999999999999 và 13.9500000000001. Kết quả là không tốt và chuyển đổi
round(12.54673, 2)
# Returns: 12.55
9 không thể đảo ngược với Numpy. Mặt khác:
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# 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 được định dạng để mọi chữ số đều quan trọng; Trình tự là không có khoảng trống và chuyển đổi có thể đảo ngược. Đơn giản: Nếu bạn có thể có số Numpy.Loat64, hãy chuyển đổi nó thành phao bình thường để được định dạng cho con người, không phải cho bộ xử lý số, nếu không thì không có gì cần thiết hơn với Python 2.7+.
The formatting of
round(12.54673, 2)
# Returns: 12.55
6 before Python 2.7 was similar to the current
round(12.54673, 2)
# Returns: 12.55
7. Both types use the same 64 bit IEEE 754 double precision with 52 bit mantissa. A big difference is that
round(12.54673, 2)
# Returns: 12.55
8 is formatted frequently with an excessive decimal number so that no bit can be lost, but no valid IEEE 754 number exists between 13.949999999999999 and 13.950000000000001. The result is not nice and the conversion
round(12.54673, 2)
# Returns: 12.55
9 is not reversible with numpy. On the other hand:
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# 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 is formatted so that every digit is important; the sequence is without gaps and the conversion is reversible. Simply: If you perhaps have a numpy.float64 number, convert it to normal float in order to be formatted for humans, not for numeric processors, otherwise nothing more is necessary with Python 2.7+.

Python có một số tùy chọn để làm tròn đến một số toàn bộ. Nhưng điều gì sẽ xảy ra nếu chúng ta muốn làm tròn đến một số chữ số thập phân, như 2 chữ số thập phân? Hãy để xem cách chúng ta làm điều đó.

Trong bài viết này:

  • Giá trị python tròn cho hai vị trí số thập phân (và hơn thế nữa)
  • Vòng thập phân tròn lên xuống:
    # Some numerical values
    valueA = 3.14159265359
    valueB = 1845.7409947
    valueC = -100.95
    valueD = 9.5432
    valueE = 34.49953
    
    # Round to different decimal places
    roundA = round(valueA, 5)
    roundB = round(valueB, 4)
    roundC = round(valueC, 3)
    roundD = round(valueD, 2)
    roundE = round(valueE, 1)
    
    # 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
    • Ví dụ: Các chữ số thập phân tròn lên xuống
  • Vòng thập phân ở Python
    • Ví dụ: Làm tròn đến 2 chữ số thập phân
  • Vòng thập phân tròn xuống ở Python
    • Ví dụ: Làm tròn xuống 2 chữ số thập phân
  • Vòng thập phân của danh sách và mảng
    • Danh sách các vòng đến các vị trí thập phân với danh sách hiểu biết
    • Danh sách tròn đến các vị trí thập phân với vòng lặp Python
    • Mảng Python tròn đến 2 chữ số thập phân
    • Vòng numpy mảng đến hai chữ số thập phân
  • Bản tóm tắt

# Vòng Python giá trị đến hai vị trí số thập phân (và hơn thế nữa)

Mặc dù làm tròn vào một số toàn bộ là hữu ích, nhưng vẫn có rất nhiều tình huống mà chúng ta cần phải làm tròn đến một số vị trí thập phân nhất định. Ví dụ, tiền tệ được báo cáo tốt nhất với 2 chữ số thập phân. Và mức độ lỗi và ý nghĩa thường sử dụng nhiều chữ số hơn, như 5.

Python có một số cách để các chữ số thập phân tròn:

  • Hàm
    # Some numerical values
    valueA = 3.14159265359
    valueB = 1845.7409947
    valueC = -100.95
    valueD = 9.5432
    valueE = 34.49953
    
    # Round to different decimal places
    roundA = round(valueA, 5)
    roundB = round(valueB, 4)
    roundC = round(valueC, 3)
    roundD = round(valueD, 2)
    roundE = round(valueE, 1)
    
    # 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 số thập phân lên xuống. Điều này làm cho
    # Some numerical values
    valueA = 3.14159265359
    valueB = 1845.7409947
    valueC = -100.95
    valueD = 9.5432
    valueE = 34.49953
    
    # Round to different decimal places
    roundA = round(valueA, 5)
    roundB = round(valueB, 4)
    roundC = round(valueC, 3)
    roundD = round(valueD, 2)
    roundE = round(valueE, 1)
    
    # 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 thành
    # Some numerical values
    valueA = 3.14159265359
    valueB = 1845.7409947
    valueC = -100.95
    valueD = 9.5432
    valueE = 34.49953
    
    # Round to different decimal places
    roundA = round(valueA, 5)
    roundB = round(valueB, 4)
    roundC = round(valueC, 3)
    roundD = round(valueD, 2)
    roundE = round(valueE, 1)
    
    # 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à
    # Some numerical values
    valueA = 3.14159265359
    valueB = 1845.7409947
    valueC = -100.95
    valueD = 9.5432
    valueE = 34.49953
    
    # Round to different decimal places
    roundA = round(valueA, 5)
    roundB = round(valueB, 4)
    roundC = round(valueC, 3)
    roundD = round(valueD, 2)
    roundE = round(valueE, 1)
    
    # 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 thành
    # Some numerical values
    valueA = 3.14159265359
    valueB = 1845.7409947
    valueC = -100.95
    valueD = 9.5432
    valueE = 34.49953
    
    # Round to different decimal places
    roundA = round(valueA, 5)
    roundB = round(valueB, 4)
    roundC = round(valueC, 3)
    roundD = round(valueD, 2)
    roundE = round(valueE, 1)
    
    # 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 các vị trí thập phân, chúng ta phải sử dụng một chức năng tùy chỉnh. Theo cách đó
    # Some numerical values
    valueA = 3.14159265359
    valueB = 1845.7409947
    valueC = -100.95
    valueD = 9.5432
    valueE = 34.49953
    
    # Round to different decimal places
    roundA = round(valueA, 5)
    roundB = round(valueB, 4)
    roundC = round(valueC, 3)
    roundD = round(valueD, 2)
    roundE = round(valueE, 1)
    
    # 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 trở thành
    # Some numerical values
    valueA = 3.14159265359
    valueB = 1845.7409947
    valueC = -100.95
    valueD = 9.5432
    valueE = 34.49953
    
    # Round to different decimal places
    roundA = round(valueA, 5)
    roundB = round(valueB, 4)
    roundC = round(valueC, 3)
    roundD = round(valueD, 2)
    roundE = round(valueE, 1)
    
    # 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)
    
    9.
  • Để làm tròn các vị trí thập phân xuống, chúng tôi sử dụng một chức năng tùy chỉnh. Điều đó biến
    Value:          Rounded:
    3.14159265359   3.14159
    1845.7409947    1845.741
    -100.95         -100.95
    9.5432          9.54
    34.49953        34.5
    
    0 thành
    Value:          Rounded:
    3.14159265359   3.14159
    1845.7409947    1845.741
    -100.95         -100.95
    9.5432          9.54
    34.49953        34.5
    
    1.

Hãy cùng xem xét từng cách tiếp cận và mã Python mà họ yêu cầu.

# Vòng thập phân vị trí lên xuống: # Some numerical values valueA = 3.14159265359 valueB = 1845.7409947 valueC = -100.95 valueD = 9.5432 valueE = 34.49953 # Round to different decimal places roundA = round(valueA, 5) roundB = round(valueB, 4) roundC = round(valueC, 3) roundD = round(valueD, 2) roundE = round(valueE, 1) # 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

Với chức năng Python từ

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# 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, chúng ta có thể làm tròn các vị trí thập phân lên xuống. Các chức năng cần hai đối số cho điều đó. Đầu tiên là giá trị để làm tròn. Thứ hai là số chữ số sau dấu thập phân (
Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
4) để làm tròn đến (Lutz, 2013; Sweigart, 2015).

Vì vậy, để làm tròn đến 2 số thập phân chúng tôi làm:

round(12.54673, 2)
# Returns: 12.55

# Ví dụ: Các chữ số thập phân tròn lên xuống

Để xem cách thức hoạt động

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# 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 trong thực tế, hãy để khám phá chương trình nhỏ sau:

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# 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)

Mã này đầu tiên làm cho năm biến dấu phẩy động. Mỗi số có một số số sau điểm thập phân (

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
4). Chúng tôi lưu trữ các giá trị của chúng trong các biến
Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
7 đến
Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
8.

Sau đó, chúng tôi làm tròn mỗi nơi đến một số vị trí thập phân. Chúng tôi gọi hàm

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# 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 với hai đối số. Đầu tiên là biến chúng tôi vừa thực hiện. Số lượng số thập phân để làm tròn đến. Ở đây chúng tôi sử dụng các giá trị
round_decimals_up(8.343)
# Returns: 8.35
0 đến
round_decimals_up(8.343)
# Returns: 8.35
1 cho điều đó. Chúng tôi lưu trữ các giá trị tròn trong các biến mới (
round_decimals_up(8.343)
# Returns: 8.35
2 đến
round_decimals_up(8.343)
# Returns: 8.35
3).

Sau đó, hàm Python từ

round_decimals_up(8.343)
# Returns: 8.35
4 hiển thị cả giá trị ban đầu và tròn. Với phương thức chuỗi
round_decimals_up(8.343)
# Returns: 8.35
5, chúng tôi sắp xếp giá trị ban đầu sang trái. Điều đó làm cho một đầu ra giống như bàn đẹp. Như chúng ta có thể thấy từ đầu ra, mỗi giá trị được làm tròn chính xác:

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5

# Vòng thập phân ở Python

Một lựa chọn khác là luôn luôn làm tròn một số chữ số thập phân. Đó là ví dụ như những gì các công ty làm: thay vì giá tròn lên hoặc giảm, các giá trị như

round_decimals_up(8.343)
# Returns: 8.35
6 thay vào đó luôn được làm tròn lên tới 4,55 đô la.

Python, tuy nhiên, không có chức năng tích hợp cho điều đó. Nhưng chúng ta có thể làm cho riêng mình và tận dụng chức năng

round_decimals_up(8.343)
# Returns: 8.35
7. Khi chúng ta làm, sử dụng chức năng trông như thế này:

round_decimals_up(8.343)
# Returns: 8.35

Ở đây, cách chúng tôi mã hóa chức năng

round_decimals_up(8.343)
# Returns: 8.35
8:

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor

Hàm này có hai tham số: giá trị thành vòng (

round_decimals_up(8.343)
# Returns: 8.35
9) và có bao nhiêu vị trí thập phân cần có (
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
0). Khi tham số sau đó được đặt, nó mặc định là
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
1.

Mã chức năng có hai phần. Đầu tiên xác nhận và xử lý tham số

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
0. Thứ hai thực hiện làm tròn thực tế.

Đối với xác nhận đó, chúng tôi sử dụng câu lệnh của Python đã được đưa ra. Điều kiện đầu tiên có

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
3 xem tham số
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
0 có phải là một cái gì đó khác hơn một số nguyên (
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
5). Khi có, chúng tôi tạo ra
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
6 để thông báo cho người dùng rằng chức năng được sử dụng sai cách.

Tiếp theo, chúng tôi kiểm tra xem

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
0 có nằm dưới (
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
8) không. Vì hàm không biết cách xử lý các vị trí thập phân âm, chúng tôi kích hoạt
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
9 để truyền đạt điều này với người dùng chức năng.

Thử nghiệm

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


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

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
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 cuối cùng xem nếu tham số ____70 bằng (
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


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

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
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)
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


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

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
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. Khi hàm phải làm tròn một giá trị mà không có vị trí thập phân, chúng ta chỉ có thể thực hiện hàm
round_decimals_up(8.343)
# Returns: 8.35
7 và làm tròn đến một số toàn bộ.

Sau đó, chúng ta đến phần thứ hai của hàm. Đây là nơi chúng tôi làm tròn thực tế đến các vị trí thập phân. Nhưng trước tiên chúng tôi xác định hệ số hiệu chỉnh. Để có được giá trị đó, chúng tôi nâng

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


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

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
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ên sức mạnh của tham số
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor
0. Chúng tôi lưu trữ kết quả trong biến
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


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

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
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.

Tiếp theo chúng tôi gọi hàm

round_decimals_up(8.343)
# Returns: 8.35
7. Bên trong dấu ngoặc đơn của nó, chúng tôi chỉ định giá trị để làm tròn (
round_decimals_up(8.343)
# Returns: 8.35
9) nhân với
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


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

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
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. Sau đó, chúng tôi chia kết quả tròn với
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


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

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
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ấy lại số lượng thập phân thích hợp.

Giả sử chức năng nên tròn

Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
2 đến 2 thập phân. Mã sau đó lần đầu tiên nhân
Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
2 với
Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
4 (102).
round_decimals_up(8.343)
# Returns: 8.35
7 sau đó làm tròn
Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
6 lên đến một số nguyên (
Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
7). Cuối cùng, chúng tôi chia trở lại với
Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
4 để có được kết quả thành hai chữ số thập phân:
Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
9.

# Ví dụ: Làm tròn đến 2 chữ số thập phân

Để xem cách tương tác với hàm

round_decimals_up(8.343)
# Returns: 8.35
8 tùy chỉnh đó, hãy để sử dụng nó trong một chương trình nhỏ:

import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


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

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
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)

Chương trình này lần đầu tiên nhập mô -đun

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
01. Điều đó làm cho chức năng
round_decimals_up(8.343)
# Returns: 8.35
7 có sẵn cho chức năng tùy chỉnh của chúng tôi, chúng tôi sao chép/dán vào chương trình tiếp theo.

Sau đó, chúng tôi thực hiện năm biến (

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
7 đến
Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
8). Mỗi người có một giá trị điểm nổi với một vài chữ số thập phân. Hãy để vòng tròn giá trị của họ.

Vì vậy, chúng tôi gọi hàm

round_decimals_up(8.343)
# Returns: 8.35
8 nhiều lần. Chúng tôi cung cấp cho nó một đối số: giá trị thành vòng. Hàm sau đó tự động sử dụng hai vị trí thập phân để làm tròn. Chúng tôi đặt các giá trị tròn trong các biến
round_decimals_up(8.343)
# Returns: 8.35
2 đến
round_decimals_up(8.343)
# Returns: 8.35
3.

Hàm

round_decimals_up(8.343)
# Returns: 8.35
4 sau đó xuất ra giá trị ban đầu và tròn. Đây là cách chức năng làm tròn mọi biến lên đến hai chữ số thập phân:

Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5

# Vòng thập phân ở Python

Cách thứ ba để xử lý các chữ số thập phân là luôn luôn làm tròn. Đây là những gì một giáo viên nghiêm ngặt có thể sử dụng: thay vì vòng

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
09 và
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
10, thay vào đó anh ấy hoặc cô ấy tròn xuống
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
11 và
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
12.

Không có chức năng tích hợp trong Python cho loại làm tròn đó. Nhưng chúng ta có thể xây dựng của riêng mình và tận dụng chức năng

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
13. Sử dụng một chức năng tùy chỉnh như vậy trông giống như:

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
0

Điều đó hoạt động độc đáo. Bây giờ, hãy để mã mà chức năng tùy chỉnh đó:

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
1

Phần lớn chức năng này giống như hàm tùy chỉnh khác (

round_decimals_up(8.343)
# Returns: 8.35
8) mà chúng ta đã thảo luận trước đó.

Sự khác biệt chính là bây giờ chúng tôi sử dụng chức năng

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
13. Hàm này làm tròn tham số
round_decimals_up(8.343)
# Returns: 8.35
9 nhân với biến
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


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

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
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 xuống. Sau đó, chúng tôi chia lại với
import math


def round_decimals_up(number:float, decimals:int=2):
    """
    Returns a value rounded up to a specific number of decimal places.
    """
    if not isinstance(decimals, int):
        raise TypeError("decimal places must be an integer")
    elif decimals < 0:
        raise ValueError("decimal places has to be 0 or more")
    elif decimals == 0:
        return math.ceil(number)

    factor = 10 ** decimals
    return math.ceil(number * factor) / factor


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

# Round to values to a different number of decimal places
roundA = round_decimals_up(valueA)
roundB = round_decimals_up(valueB)
roundC = round_decimals_up(valueC)
roundD = round_decimals_up(valueD)
roundE = round_decimals_up(valueE)

# Output the results
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ó được số lượng thập phân phù hợp.

Giả sử chúng tôi yêu cầu chức năng tròn

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
19 xuống còn 2 chữ số thập phân. Mã sau đó nhân lên giá trị đó với
Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
4.
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
13 sau đó làm tròn
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
22 xuống một số toàn bộ (
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
23). Khi chúng tôi chia trở lại với
Value:          Rounded:
3.14159265359   3.15
1845.7409947    1845.75
-100.95         -100.95
9.5432          9.55
34.49953        34.5
4, chúng tôi sẽ nhận được số lượng số thập phân thích hợp:
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
25.

# Ví dụ: Làm tròn xuống 2 chữ số thập phân

Hãy để xem làm thế nào mà chức năng trên hoạt động trong thực tế. Chương trình nhỏ này có chức năng làm tròn 5 giá trị khác nhau:

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
2

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

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
01. Bằng cách đó, hàm
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
27 tùy chỉnh có thể sử dụng
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
13. Sau đó chúng tôi chèn chức năng tùy chỉnh đó.

Sau đó, chúng tôi thực hiện năm biến (

Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
7 đến
Value:          Rounded:
3.14159265359   3.14159
1845.7409947    1845.741
-100.95         -100.95
9.5432          9.54
34.49953        34.5
8). Mỗi người giữ một giá trị với số thập phân để làm tròn xuống. Đó là những gì chúng tôi làm tiếp theo.

Vì vậy, chúng tôi liên tục thực hiện

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
27. Mỗi lần chúng tôi đưa ra hàm hai đối số: giá trị thành vòng và số chữ số thập phân để giữ. Chúng tôi đặt kết quả trong các biến mới (
round_decimals_up(8.343)
# Returns: 8.35
2 đến
round_decimals_up(8.343)
# Returns: 8.35
3).

Bit cuối cùng của mã đầu ra kết quả với hàm

round_decimals_up(8.343)
# Returns: 8.35
4. Chúng tôi đã đưa ra giá trị ban đầu bằng phương pháp
round_decimals_up(8.343)
# Returns: 8.35
5 và cũng bao gồm giá trị tròn. Khi đầu ra hiển thị, mỗi giá trị được làm tròn xuống một số vị trí thập phân nhất định:

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
3

# Vòng thập phân của danh sách và mảng Python

Các ví dụ trên luôn làm tròn một giá trị tại một thời điểm. Tuy nhiên, đôi khi, chúng tôi có một chuỗi với các giá trị mà chúng tôi muốn làm tròn đến một số chữ số thập phân. Hãy cùng xem cách làm như vậy trong Python.

# Danh sách các vòng đến các vị trí thập phân với danh sách hiểu biết

Một tùy chọn là làm tròn mọi giá trị với sự hiểu biết danh sách. Điều này chỉ cần một chút mã và chạy hiệu quả. Một danh sách hiểu hoạt động tốt nhất khi mã của bạn không phức tạp. Đơn giản chỉ cần làm tròn các giá trị đáp ứng dấu hiệu đó.

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

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
4

Danh sách

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
36 mà chúng tôi thực hiện ở đây có được các giá trị của nó từ một danh sách hiểu. Sự hiểu biết đó thực thi biểu thức
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
37 cho mọi giá trị được tạo bởi vòng lặp
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# 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 nội tuyến:
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
39. Đó là cách mà chúng ta đi qua danh sách
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
40 ban đầu cùng một yếu tố.

Ở đây, một chương trình nhỏ làm tròn mọi giá trị trong danh sách:

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
5

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

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
01. Sau đó, chúng tôi chèn các chức năng
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
27 và
round_decimals_up(8.343)
# Returns: 8.35
8 mà chúng tôi đã thực hiện trước đó trong bài viết này.

Tiếp theo chúng tôi lập một danh sách với các con số. Danh sách

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
40 đó có 6 giá trị điểm nổi. Sau đó, chúng tôi làm tròn từng giá trị trong danh sách đó. Chúng tôi sử dụng ba toàn bộ danh sách khác nhau cho điều đó.

Các vòng đầu tiên mọi giá trị danh sách đến hai vị trí thập phân (lên và xuống) với hàm

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# 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. Chúng tôi đặt các giá trị tròn đó vào một danh sách mới,
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
46.

Độ hiểu hiểu danh sách thứ hai có hàm

round_decimals_up(8.343)
# Returns: 8.35
8 làm tròn đến hai chữ số thập phân. Danh sách mới mà lợi nhuận này là những gì chúng tôi lưu trữ trong
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
48. Các vòng hiểu danh sách thứ ba và cuối cùng xuống đến hai vị trí thập phân (
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
27). Kết quả của sự hiểu biết đó được lưu trữ trong danh sách
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
50.

Chương trình Bit Bit của mã đầu ra cuối cùng tất cả bốn danh sách với chức năng

round_decimals_up(8.343)
# Returns: 8.35
4. Đây là cách các giá trị tròn đó trông như thế nào:

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
6

Trong ví dụ trên, chúng tôi đã thực hiện các danh sách mới với danh sách hiểu. Nếu bạn không cần phải giữ các giá trị danh sách ban đầu, thì việc hiểu danh sách cũng có thể ghi đè lên danh sách hiện có. Để làm như vậy, chúng tôi đặt giá trị danh sách thành kết của kết quả của danh sách hiểu:

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
7

# Danh sách tròn đến các vị trí thập phân với vòng lặp Python # Some numerical values valueA = 3.14159265359 valueB = 1845.7409947 valueC = -100.95 valueD = 9.5432 valueE = 34.49953 # Round to different decimal places roundA = round(valueA, 5) roundB = round(valueB, 4) roundC = round(valueC, 3) roundD = round(valueD, 2) roundE = round(valueE, 1) # 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

Cách thứ hai để các giá trị danh sách tròn là với vòng lặp

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# 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. Điều này đòi hỏi nhiều mã hơn một chút so với việc hiểu danh sách, nhưng có nhiều khả năng hơn và dễ đọc hơn trong các tình huống phức tạp.

Như một ví dụ nhanh:

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
8

Danh sách

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
54 chúng tôi thực hiện ở đây bắt đầu trống (
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
55). Nhưng bên trong vòng lặp
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# 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, chúng tôi điền vào danh sách đó bằng các giá trị. Vì vậy, chúng tôi gọi phương thức
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
57 trong danh sách và chuyển trong giá trị tròn từ danh sách ban đầu (
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
37).

Chương trình nhỏ này làm tròn danh sách các giá trị với

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# 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:

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
9

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

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
01. Sau đó, chúng tôi sao chép/dán các chức năng
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
27 và
round_decimals_up(8.343)
# Returns: 8.35
8 mà chúng tôi đã thực hiện trước đó trong bài viết này.

Sau đó, chúng tôi lập một danh sách với một số giá trị dấu phẩy động, cả tích cực và tiêu cực. Sau đó, chúng tôi mã hóa ba danh sách trống (

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
55):
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
46,
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
48 và
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
50. Chúng sẽ giữ các giá trị tròn.

Để thực hiện điều đó, chúng tôi tạo ra một vòng lặp

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# 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. Vòng lặp này lặp qua mọi giá trị trong danh sách
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
40. Trong mỗi chu kỳ vòng lặp, giá trị danh sách hiện tại được truy cập với biến vòng lặp
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
69. Bên trong vòng lặp đó, chúng tôi nối một giá trị tròn vào mỗi trong ba danh sách với phương thức Python từ
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
57.

Chương trình Bit Bit của mã cuối cùng sau đó xuất ra danh sách ban đầu và các dẫn xuất tròn của nó. Ở đây, những gì hiển thị:

import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.
0

Không cần các giá trị danh sách ban đầu? Trong trường hợp đó, bạn không phải lập một danh sách mới, riêng biệt. Thay vào đó, bạn có thể ghi đè lên một nơi hiện có. Một chức năng hữu ích cho điều đó là Python từ

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
71. Ví dụ:

import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.
1

# Mảng Python tròn đến 2 chữ số thập phân

Tất nhiên chúng ta cũng có thể làm tròn các bộ sưu tập khác hơn danh sách. Ở đây, cách chúng ta làm tròn mọi giá trị trong một mảng Python đến 2 số thập phân:

import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.
2

Đầu tiên chúng tôi nhập hàm tạo

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
72 từ mô -đun
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
73. Sau đó, chúng tôi tạo một mảng với các giá trị dấu phẩy động. Chúng tôi đặt tên cho mảng đó
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
40.

Sau đó, chúng tôi xây dựng một mảng thứ hai. Cái này có các giá trị từ mảng ban đầu được làm tròn đến 2 thập phân. Để thực hiện điều đó, chúng tôi sử dụng một danh sách hiểu. Đó là làm tròn từng giá trị thành hai chữ số thập phân (

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
37). Chúng tôi đặt tên cho mảng mới, tròn
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
36.

Hàm

round_decimals_up(8.343)
# Returns: 8.35
4 sau đó xuất ra mảng gốc và tròn. Như chúng ta có thể nói, mỗi giá trị được làm tròn đúng đến hai vị trí thập phân:

import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.
3

Bạn có cần phải giữ các giá trị mảng ban đầu không? Trong trường hợp đó, bạn có thể ghi đè lên mảng hiện có với các giá trị tròn của nó. Đây là cách: làm thế nào:

import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.
4

# Vòng numpy mảng đến hai chữ số thập phân

Tất nhiên chúng ta cũng có thể làm tròn các giá trị của các mảng khác. Một ví dụ là những người từ gói lập trình số numpy cho Python. Để làm tròn một mảng numpy đến hai vị trí thập phân chúng tôi làm:

import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.
5

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

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
78. Sau đó, chúng tôi tạo ra một mảng numpy với hàm
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
79. Mảng đó có một số giá trị dấu phẩy động.

Sau đó, chúng tôi làm tròn mảng ban đầu đến hai chữ số thập phân. Vì vậy, chúng tôi gọi hàm

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
80 với hai đối số. Đầu tiên là mảng có giá trị đến tròn. Thứ hai là số lượng vị trí thập phân để làm tròn đến. Chúng tôi lưu trữ mảng hàm trả về trong biến
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
36.

Bit cuối cùng của mã đầu ra cả mảng gốc và tròn. Điều đó cho thấy cách các giá trị được làm tròn độc đáo đến 2 thập phân:

import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.
6

Hàm

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
80 làm tròn lên xuống. Để luôn làm tròn hoặc luôn làm tròn xuống, chúng tôi phải sử dụng các chức năng
round_decimals_up(8.343)
# Returns: 8.35
8 và
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
27 tùy chỉnh.

Khi chúng tôi sử dụng các hàm đó trong một danh sách hiểu, chúng có thể làm tròn từng giá trị từ một mảng numpy. Ví dụ:

import random
from decimal import Decimal
for _ in range(1000000):
    x = random.random()
    assert x == float(repr(x)) == float(Decimal(repr(x)))  # Reversible repr()
    assert str(x) == repr(x)
    assert len(repr(round(x, 12))) <= 14         # no excessive decimal places.
7

ĐỌC THÊM

  • Vòng python giá trị cho một số toàn bộ.
  • Cắt ngắn giá trị python thành một số
  • Cắt ngắn đến một số vị trí thập phân nhất định trong Python

# Bản tóm tắt

Có ba cách để số tròn đến một số vị trí thập phân nhất định. Để làm tròn và xuống, chúng tôi sử dụng chức năng Python từ ____41. Đối số đầu tiên chúng tôi đưa ra chức năng đó là số đến tròn. Đối số thứ hai số lượng vị trí thập phân để làm tròn đến.

Python không có chức năng luôn làm tròn các chữ số thập phân (

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
86 thành
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
87). Nhưng khi chúng ta tận dụng chức năng
round_decimals_up(8.343)
# Returns: 8.35
7 trong một chức năng tùy chỉnh, chúng ta có thể dễ dàng thực hiện điều đó.

Không có chức năng nào luôn làm tròn các chữ số thập phân (

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
89 trở thành
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
90). Nhưng khi chúng ta bọc chức năng
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
(0.1, 0.2, 0.3)
13 trong một hàm tùy chỉnh, chúng ta có thể tự tạo hành vi đó.

Để làm tròn mọi giá trị trong danh sách hoặc mảng, chúng ta có thể sử dụng vòng lặp hiểu biết hoặc vòng lặp

# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953

# Round to different decimal places
roundA = round(valueA, 5)
roundB = round(valueB, 4)
roundC = round(valueC, 3)
roundD = round(valueD, 2)
roundE = round(valueE, 1)

# 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. Đầu tiên là nhỏ gọn và lần thứ hai dễ làm việc hơn.

Người giới thiệu

Lutz, M. (2013). Học Python (Phiên bản thứ 5). Sebastopol, CA: O hèReilly Media.

Sweigart, A. (2015). Tự động hóa những thứ nhàm chán với Python: Lập trình thực tế cho toàn bộ người mới bắt đầu. San Francisco, CA: Không có báo chí tinh bột.

Python.org (N.D.). Chức năng tích hợp sẵn. Truy cập vào ngày 8 tháng 11 năm 2019, từ https://docs.python.org/3/l Library/fives.html

Cập nhật lần cuối vào ngày 9 tháng 9 năm 2020 (xuất bản ngày 20 tháng 12 năm 2019).

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