sử dụng str. định dạng[] với “{. 2f}” dưới dạng chuỗi và float dưới dạng số để hiển thị 2 chữ số thập phân trong Python. Gọi in và nó sẽ hiển thị số float với 2 chữ số thập phân trong bảng điều khiển
Nội dung chính Hiển thị
Mã ví dụ đơn giản sử dụng str. định dạng [số] với “{. 2f}” dưới dạng chuỗi và float dưới dạng số để trả về biểu diễn chuỗi của số có hai chữ số thập phân
fnum = 7.154327
res = "{:.2f}".format[fnum]
print[res]
đầu ra
Làm cách nào để hiển thị số float có hai chữ số thập phân?
Câu trả lời. Sử dụng toán tử định dạng chuỗi cho điều đó
>>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
HOẶC
print["{:.2f}".format[5]]
Hãy bình luận nếu bạn có bất kỳ nghi ngờ và đề xuất nào về mã vị trí thập phân Python này
Ghi chú. IDE. PyCharm 2021. 3. 3 [Phiên bản cộng đồng]
cửa sổ 10
Trăn 3. 10. 1
Tất cả các Ví dụ về Python đều nằm trong Python 3, vì vậy có thể nó khác với python 2 hoặc các phiên bản nâng cấp
Bằng cấp về Khoa học Máy tính và Kỹ sư. Nhà phát triển ứng dụng và có kinh nghiệm về nhiều ngôn ngữ lập trình. Đam mê công nghệ & thích học hỏi kỹ thuật
Nếu bạn đang sử dụng giá trị này cho tiền tệ và cũng muốn giá trị được phân tách bằng ________ 34, bạn có thể sử dụng
>>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
5e. g
>>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
6>>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
5 >>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
8 >>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
9Đây là một chút mã tôi đã viết cách đây một thời gian
>>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
0> At the end of year 1 total paid is $ 43,806.36
> At the end of year 2 total paid is $ 87,612.72
> At the end of year 3 total paid is $ 131,419.08
> At the end of year 4 total paid is $ 175,225.44
> At the end of year 5 total paid is $ 219,031.80 At the end of year 6 total paid is $ 262,838.16
> At the end of year 7 total paid is $ 306,644.52
> At the end of year 8 total paid is $ 350,450.88
> At the end of year 9 total paid is $ 394,257.24
> At the end of year 10 total paid is $ 438,063.60 At the end of year 11 total paid is $ 481,869.96
> At the end of year 12 total paid is $ 525,676.32
> At the end of year 13 total paid is $ 569,482.68
> At the end of year 14 total paid is $ 613,289.04
> At the end of year 15 total paid is $ 657,095.40 At the end of year 16 total paid is $ 700,901.76
> At the end of year 17 total paid is $ 744,708.12
> At the end of year 18 total paid is $ 788,514.48
> At the end of year 19 total paid is $ 832,320.84
> At the end of year 20 total paid is $ 876,127.20 > '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
1 là >>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
2, do đó đầu ra cũng mang lại giá trị lên tới >>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
3 chữ số thập phân. Tuy nhiên, bạn muốn tạo một đầu ra lên đến 14 chữ số thập phân, tôi. e. , >>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
5Do đó, không chậm trễ hơn nữa, chúng ta hãy đi sâu vào các giải pháp cho vấn đề của mình
💡 Phương pháp 1. Sử dụng định dạng chuỗi
Python chủ yếu có 3 cách định dạng chuỗi. Đó là -
6>>> '%.2f' % 1.234 '1.23' >>> '%.2f' % 5.0 '5.00'
7 [Chuỗi ký tự]>>> '%.2f' % 1.234 '1.23' >>> '%.2f' % 5.0 '5.00'
- Định dạng
8>>> '%.2f' % 1.234 '1.23' >>> '%.2f' % 5.0 '5.00'
Hãy xem cách chúng ta có thể sử dụng các phương thức này để định dạng giá trị dấu phẩy động thành hai chữ số thập phân
1️⃣ đường. định dạng[]
9 là một phương thức trong Python định dạng các giá trị cụ thể và chèn chúng vào trình giữ chỗ>>> '%.2f' % 1.234 '1.23' >>> '%.2f' % 5.0 '5.00'
0 của chuỗiprint["{:.2f}".format[5]]
- Nó trả về chuỗi đã định dạng
cú pháp
print["{:.2f}".format[5]]
1Bạn có thể sử dụng
print["{:.2f}".format[5]]
2 cùng với print["{:.2f}".format[5]]
3 bên trong trình giữ chỗ, tôi. e. , print["{:.2f}".format[5]]
4. Điều này trả về một chuỗi đại diện của số lên đến hai chữ số thập phân, như được hiển thị bên dướiDung dịch
nhập toán học
bán kính = 5
diện tích = toán. pi*bán kính*bán kính
in["Diện tích = {. 2f}". định dạng[khu vực]]
đầu ra
Diện tích = 78. 54
📌Một cách khác để sử dụng toán tử định dạng để in đến hai chữ số thập phân như sau
print["{:.2f}".format[5]]
5⚠️Chú ý.
>>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
6 đã được giới thiệu trong Python 2. 6. Do đó, bạn không thể sử dụng nó trong các phiên bản trước Python 2. 62️⃣ chuỗi f
Một
>>> '%.2f' % 1.234
'1.23'
>>> '%.2f' % 5.0
'5.00'
7 là một chuỗi ký tự Python có tiền tố 'print["{:.2f}".format[5]]
8' chứa các biểu thức bên trong dấu ngoặc nhọn. Các biểu thức này có thể được thay thế bằng các giá trị của chúngDo đó, bạn có thể sử dụng
print["{:.2f}".format[5]]
9 để trả về một chuỗi đại diện cho số có đến hai chữ số thập phânDung dịch
nhập toán học
bán kính = 5
diện tích = toán. pi*bán kính*bán kính
in[f"Diện tích = {diện tích. 2f}"]
đầu ra
Diện tích = 78. 54
3️⃣ % định dạng
Dung dịch
nhập toán học
bán kính = 5
diện tích = toán. pi *bán kính *bán kính
in["Diện tích = %. 2f"%diện tích]
đầu ra
Diện tích = 78. 54
💡 Phương pháp 2. Sử dụng hàm round[]
> At the end of year 1 total paid is $ 43,806.36
> At the end of year 2 total paid is $ 87,612.72
> At the end of year 3 total paid is $ 131,419.08
> At the end of year 4 total paid is $ 175,225.44
> At the end of year 5 total paid is $ 219,031.80 At the end of year 6 total paid is $ 262,838.16
> At the end of year 7 total paid is $ 306,644.52
> At the end of year 8 total paid is $ 350,450.88
> At the end of year 9 total paid is $ 394,257.24
> At the end of year 10 total paid is $ 438,063.60 At the end of year 11 total paid is $ 481,869.96
> At the end of year 12 total paid is $ 525,676.32
> At the end of year 13 total paid is $ 569,482.68
> At the end of year 14 total paid is $ 613,289.04
> At the end of year 15 total paid is $ 657,095.40 At the end of year 16 total paid is $ 700,901.76
> At the end of year 17 total paid is $ 744,708.12
> At the end of year 18 total paid is $ 788,514.48
> At the end of year 19 total paid is $ 832,320.84
> At the end of year 20 total paid is $ 876,127.20 At the end of year 1 total paid is $ 43,806.36
> At the end of year 2 total paid is $ 87,612.72
> At the end of year 3 total paid is $ 131,419.08
> At the end of year 4 total paid is $ 175,225.44
> At the end of year 5 total paid is $ 219,031.80 At the end of year 6 total paid is $ 262,838.16
> At the end of year 7 total paid is $ 306,644.52
> At the end of year 8 total paid is $ 350,450.88
> At the end of year 9 total paid is $ 394,257.24
> At the end of year 10 total paid is $ 438,063.60 At the end of year 11 total paid is $ 481,869.96
> At the end of year 12 total paid is $ 525,676.32
> At the end of year 13 total paid is $ 569,482.68
> At the end of year 14 total paid is $ 613,289.04
> At the end of year 15 total paid is $ 657,095.40 At the end of year 16 total paid is $ 700,901.76
> At the end of year 17 total paid is $ 744,708.12
> At the end of year 18 total paid is $ 788,514.48
> At the end of year 19 total paid is $ 832,320.84
> At the end of year 20 total paid is $ 876,127.20 At the end of year 1 total paid is $ 43,806.36
> At the end of year 2 total paid is $ 87,612.72
> At the end of year 3 total paid is $ 131,419.08
> At the end of year 4 total paid is $ 175,225.44
> At the end of year 5 total paid is $ 219,031.80 At the end of year 6 total paid is $ 262,838.16
> At the end of year 7 total paid is $ 306,644.52
> At the end of year 8 total paid is $ 350,450.88
> At the end of year 9 total paid is $ 394,257.24
> At the end of year 10 total paid is $ 438,063.60 At the end of year 11 total paid is $ 481,869.96
> At the end of year 12 total paid is $ 525,676.32
> At the end of year 13 total paid is $ 569,482.68
> At the end of year 14 total paid is $ 613,289.04
> At the end of year 15 total paid is $ 657,095.40 At the end of year 16 total paid is $ 700,901.76
> At the end of year 17 total paid is $ 744,708.12
> At the end of year 18 total paid is $ 788,514.48
> At the end of year 19 total paid is $ 832,320.84
> At the end of year 20 total paid is $ 876,127.20 At the end of year 2 total paid is $ 87,612.72
> At the end of year 3 total paid is $ 131,419.08
> At the end of year 4 total paid is $ 175,225.44
> At the end of year 5 total paid is $ 219,031.80 At the end of year 6 total paid is $ 262,838.16
> At the end of year 7 total paid is $ 306,644.52
> At the end of year 8 total paid is $ 350,450.88
> At the end of year 9 total paid is $ 394,257.24
> At the end of year 10 total paid is $ 438,063.60 At the end of year 11 total paid is $ 481,869.96
> At the end of year 12 total paid is $ 525,676.32
> At the end of year 13 total paid is $ 569,482.68
> At the end of year 14 total paid is $ 613,289.04
> At the end of year 15 total paid is $ 657,095.40 At the end of year 16 total paid is $ 700,901.76
> At the end of year 17 total paid is $ 744,708.12
> At the end of year 18 total paid is $ 788,514.48
> At the end of year 19 total paid is $ 832,320.84
> At the end of year 20 total paid is $ 876,127.20 At the end of year 1 total paid is $ 43,806.36
> At the end of year 2 total paid is $ 87,612.72
> At the end of year 3 total paid is $ 131,419.08
> At the end of year 4 total paid is $ 175,225.44
> At the end of year 5 total paid is $ 219,031.80 At the end of year 6 total paid is $ 262,838.16
> At the end of year 7 total paid is $ 306,644.52
> At the end of year 8 total paid is $ 350,450.88
> At the end of year 9 total paid is $ 394,257.24
> At the end of year 10 total paid is $ 438,063.60 At the end of year 11 total paid is $ 481,869.96
> At the end of year 12 total paid is $ 525,676.32
> At the end of year 13 total paid is $ 569,482.68
> At the end of year 14 total paid is $ 613,289.04
> At the end of year 15 total paid is $ 657,095.40 At the end of year 16 total paid is $ 700,901.76
> At the end of year 17 total paid is $ 744,708.12
> At the end of year 18 total paid is $ 788,514.48
> At the end of year 19 total paid is $ 832,320.84
> At the end of year 20 total paid is $ 876,127.20 At the end of year 1 total paid is $ 43,806.36
> At the end of year 2 total paid is $ 87,612.72
> At the end of year 3 total paid is $ 131,419.08
> At the end of year 4 total paid is $ 175,225.44
> At the end of year 5 total paid is $ 219,031.80 At the end of year 6 total paid is $ 262,838.16
> At the end of year 7 total paid is $ 306,644.52
> At the end of year 8 total paid is $ 350,450.88
> At the end of year 9 total paid is $ 394,257.24
> At the end of year 10 total paid is $ 438,063.60 At the end of year 11 total paid is $ 481,869.96
> At the end of year 12 total paid is $ 525,676.32
> At the end of year 13 total paid is $ 569,482.68
> At the end of year 14 total paid is $ 613,289.04
> At the end of year 15 total paid is $ 657,095.40 At the end of year 16 total paid is $ 700,901.76
> At the end of year 17 total paid is $ 744,708.12
> At the end of year 18 total paid is $ 788,514.48
> At the end of year 19 total paid is $ 832,320.84
> At the end of year 20 total paid is $ 876,127.20 At the end of year 2 total paid is $ 87,612.72
> At the end of year 3 total paid is $ 131,419.08
> At the end of year 4 total paid is $ 175,225.44
> At the end of year 5 total paid is $ 219,031.80 At the end of year 6 total paid is $ 262,838.16
> At the end of year 7 total paid is $ 306,644.52
> At the end of year 8 total paid is $ 350,450.88
> At the end of year 9 total paid is $ 394,257.24
> At the end of year 10 total paid is $ 438,063.60 At the end of year 11 total paid is $ 481,869.96
> At the end of year 12 total paid is $ 525,676.32
> At the end of year 13 total paid is $ 569,482.68
> At the end of year 14 total paid is $ 613,289.04
> At the end of year 15 total paid is $ 657,095.40 At the end of year 16 total paid is $ 700,901.76
> At the end of year 17 total paid is $ 744,708.12
> At the end of year 18 total paid is $ 788,514.48
> At the end of year 19 total paid is $ 832,320.84
> At the end of year 20 total paid is $ 876,127.20 At the end of year 1 total paid is $ 43,806.36
> At the end of year 2 total paid is $ 87,612.72
> At the end of year 3 total paid is $ 131,419.08
> At the end of year 4 total paid is $ 175,225.44
> At the end of year 5 total paid is $ 219,031.80 At the end of year 6 total paid is $ 262,838.16
> At the end of year 7 total paid is $ 306,644.52
> At the end of year 8 total paid is $ 350,450.88
> At the end of year 9 total paid is $ 394,257.24
> At the end of year 10 total paid is $ 438,063.60 At the end of year 11 total paid is $ 481,869.96
> At the end of year 12 total paid is $ 525,676.32
> At the end of year 13 total paid is $ 569,482.68
> At the end of year 14 total paid is $ 613,289.04
> At the end of year 15 total paid is $ 657,095.40 At the end of year 16 total paid is $ 700,901.76
> At the end of year 17 total paid is $ 744,708.12
> At the end of year 18 total paid is $ 788,514.48
> At the end of year 19 total paid is $ 832,320.84
> At the end of year 20 total paid is $ 876,127.20 At the end of year 1 total paid is $ 43,806.36
> At the end of year 2 total paid is $ 87,612.72
> At the end of year 3 total paid is $ 131,419.08
> At the end of year 4 total paid is $ 175,225.44
> At the end of year 5 total paid is $ 219,031.80 At the end of year 6 total paid is $ 262,838.16
> At the end of year 7 total paid is $ 306,644.52
> At the end of year 8 total paid is $ 350,450.88
> At the end of year 9 total paid is $ 394,257.24
> At the end of year 10 total paid is $ 438,063.60 At the end of year 11 total paid is $ 481,869.96
> At the end of year 12 total paid is $ 525,676.32
> At the end of year 13 total paid is $ 569,482.68
> At the end of year 14 total paid is $ 613,289.04
> At the end of year 15 total paid is $ 657,095.40 At the end of year 16 total paid is $ 700,901.76
> At the end of year 17 total paid is $ 744,708.12
> At the end of year 18 total paid is $ 788,514.48
> At the end of year 19 total paid is $ 832,320.84
> At the end of year 20 total paid is $ 876,127.20 At the end of year 1 total paid is $ 43,806.36
> At the end of year 2 total paid is $ 87,612.72
> At the end of year 3 total paid is $ 131,419.08
> At the end of year 4 total paid is $ 175,225.44
> At the end of year 5 total paid is $ 219,031.80 At the end of year 6 total paid is $ 262,838.16
> At the end of year 7 total paid is $ 306,644.52
> At the end of year 8 total paid is $ 350,450.88
> At the end of year 9 total paid is $ 394,257.24
> At the end of year 10 total paid is $ 438,063.60 At the end of year 11 total paid is $ 481,869.96
> At the end of year 12 total paid is $ 525,676.32
> At the end of year 13 total paid is $ 569,482.68
> At the end of year 14 total paid is $ 613,289.04
> At the end of year 15 total paid is $ 657,095.40 At the end of year 16 total paid is $ 700,901.76
> At the end of year 17 total paid is $ 744,708.12
> At the end of year 18 total paid is $ 788,514.48
> At the end of year 19 total paid is $ 832,320.84
> At the end of year 20 total paid is $ 876,127.20