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.
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
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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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
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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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 (
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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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 (
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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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:
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:
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.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ó (
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor
0). Khi tham số sau đó được đặt, nó mặc định là
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.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ố
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.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ó
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor
3 xem tham số
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor
0 có phải là một cái gì đó khác hơn một số nguyên (
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor
5). Khi có, chúng tôi tạo ra
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.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
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor
0 có nằm dưới (
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.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
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.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
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor# Some numbers to roundvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to values to a different number of decimal placesroundA=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 resultsprint("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 (
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor# Some numbers to roundvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to values to a different number of decimal placesroundA=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 resultsprint("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)
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor# Some numbers to roundvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to values to a different number of decimal placesroundA=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 resultsprint("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
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor# Some numbers to roundvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to values to a different number of decimal placesroundA=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 resultsprint("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ố
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor
0. Chúng tôi lưu trữ kết quả trong biến
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor# Some numbers to roundvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to values to a different number of decimal placesroundA=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 resultsprint("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
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor# Some numbers to roundvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to values to a different number of decimal placesroundA=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 resultsprint("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
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor# Some numbers to roundvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to values to a different number of decimal placesroundA=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 resultsprint("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ùy chỉnh đó, hãy để sử dụng nó trong một chương trình nhỏ:
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor# Some numbers to roundvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to values to a different number of decimal placesroundA=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 resultsprint("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). 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:
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor# Some numbers to roundvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to values to a different number of decimal placesroundA=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 resultsprint("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
importmathdefround_decimals_up(number:float,decimals:int=2):"""
Returns a value rounded up to a specific number of decimal places.
"""ifnotisinstance(decimals,int):raiseTypeError("decimal places must be an integer")elifdecimals<0:raiseValueError("decimal places has to be 0 or more")elifdecimals==0:returnmath.ceil(number)factor=10**decimalsreturnmath.ceil(number*factor)/factor# Some numbers to roundvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to values to a different number of decimal placesroundA=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 resultsprint("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)
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 đó.
# Some numerical valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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)
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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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,
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:
# 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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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.
# Some numerical valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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
Chương trình nhỏ này làm tròn danh sách các giá trị với
# Some numerical valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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)
Để thực hiện điều đó, chúng tôi tạo ra một vòng lặp
# Some numerical valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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
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ừ
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.
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 (
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.
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
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 (
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 valuesvalueA=3.14159265359valueB=1845.7409947valueC=-100.95valueD=9.5432valueE=34.49953# Round to different decimal placesroundA=round(valueA,5)roundB=round(valueB,4)roundC=round(valueC,3)roundD=round(valueD,2)roundE=round(valueE,1)# Output rounded valuesprint("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
Làm thế nào để bạn đặt các điểm thập phân trong Python?
Định dạng thập phân Python để định dạng số thập phân, chúng ta sẽ sử dụng str.format (số) trong đó một chuỗi là '{0: .3g}' và nó sẽ định dạng chuỗi với một số.Ngoài ra, nó sẽ hiển thị số có 1 số trước số thập phân và tối đa 2 số sau số thập phân.str.format(number) where a string is '{0:.3g}' and it will format string with a number. Also, it will display the number with 1 number before the decimal and up to 2 numbers after the decimal.
Làm thế nào để bạn định dạng đến 3 số thập phân trong Python?
Sử dụng hàm vòng () để làm tròn một chiếc phao đến 3 thập phân, ví dụ:Kết quả = vòng (6.36789, 3).Hàm vòng () sẽ làm tròn số điểm nổi đến 3 vị trí thập phân và sẽ trả về kết quả., e.g. result = round(6.36789, 3) . The round() function will round the floating-point number to 3 decimal places and will return the result.
Làm thế nào để bạn sử dụng .2f trong Python?
Một định dạng của.2F (Lưu ý f) có nghĩa là hiển thị số có hai chữ số sau điểm thập phân.Vì vậy, số 1 sẽ hiển thị là 1,00 và số 1.5555 sẽ hiển thị là 1,56.to display the number with two digits after the decimal point. So the number 1 would display as 1.00 and the number 1.5555 would display as 1.56 .
Làm cách nào để hiển thị 6 vị trí thập phân trong Python?
Sử dụng hàm vòng () để in phao đến 6 thập phân, ví dụ:in (tròn (my_float, 6)).Hàm vòng () sẽ làm tròn số điểm nổi đến 6 vị trí thập phân và sẽ trả về kết quả., e.g. print(round(my_float, 6)) . The round() function will round the floating-point number to 6 decimal places and will return the result.