Chuyển đổi thủ công phân số thành tỷ lệ phần trăm có thể được thực hiện bằng cách nhân số hữu tỷ với 100. Số thập phân thu được có thể được làm tròn đến số chữ số cần thiết bằng cách sử dụng toán học sẵn có. hàm round[] có cú pháp như sau
round[number, digits]
Python3
# define a number
num
=
Decimal equivalent in % 33.330
Decimal equivalent in % 33.331
Decimal equivalent in % 33.332
Decimal equivalent in % 33.333
Decimal equivalent in % 33.334
Decimal equivalent in % 33.335_______84_______
num
Decimal equivalent in % 33.338
Decimal equivalent in % 33.339
Decimal equivalent in % 33.333
Decimal equivalent in % 50.01
Decimal equivalent in % 50.02
Decimal equivalent in % 50.03
Decimal equivalent in % 50.04
Decimal equivalent in % 50.05
Decimal equivalent in % 33.333
Decimal equivalent in % 50.07
Decimal equivalent in % 50.02
Decimal equivalent in % 50.03
"{:.n%}".format[num]0
"{:.n%}".format[num]1_______19_______2
"{:.n%}".format[num]3
đầu ra
Decimal equivalent in % 33.33
Trường hợp tử số chia hết cho mẫu số thì số đó được trả về 1 chữ số thập phân
Python3
# define a number
num
=
"{:.n%}".format[num]2
Decimal equivalent in % 33.331
"{:.n%}".format[num]9
Decimal equivalent in % 33.333
Decimal equivalent in % 33.334
Decimal equivalent in % 33.335_______84_______
num
Decimal equivalent in % 33.338
Decimal equivalent in % 33.339
Decimal equivalent in % 33.333
Decimal equivalent in % 50.01
Decimal equivalent in % 50.02
Decimal equivalent in % 50.03
Decimal equivalent in % 50.04
Decimal equivalent in % 50.05
Decimal equivalent in % 33.333
Decimal equivalent in % 50.07
Decimal equivalent in % 50.02
Decimal equivalent in % 50.03
"{:.n%}".format[num]0
"{:.n%}".format[num]1_______19_______9
"{:.n%}".format[num]3
đầu ra
Decimal equivalent in % 50.0
Phương pháp 2.
các str. Phương thức format[] được sử dụng để chuyển đổi số thành tỷ lệ phần trăm, bằng cách chỉ định số chữ số cần lấy sau dấu thập phân.
"{:.n%}".format[num]
- n đại diện cho số chữ số sau dấu thập phân
- num đại diện cho số dấu phẩy động được biểu thị dưới dạng số thập phân hoặc phân số
Trường hợp đặc biệt khi mẫu số của phân số bằng 1, nghĩa là phân số bằng một giá trị nguyên.
Python3
Converting number to percentage rounding to 0 place of decimal 33% Converting number to percentage rounding to 2 place of decimal -50.00%1
Converting number to percentage rounding to 0 place of decimal 33% Converting number to percentage rounding to 2 place of decimal -50.00%2_______84_______
"{:.n%}".format[num]2
Decimal equivalent in % 50.02
Decimal equivalent in % 50.03
Converting number to percentage rounding to 0 place of decimal 33% Converting number to percentage rounding to 2 place of decimal -50.00%7
Decimal equivalent in % 50.05
Decimal equivalent in % 50.02
Decimal equivalent in % 50.03
# define a number
1# define a number
2# define a number
3# define a number
4Decimal equivalent in % 33.333
# define a number
6
Converting number to percentage rounding to 0 place of decimal 33% Converting number to percentage rounding to 2 place of decimal -50.00%2_______84_______
# define a number
9num
0Decimal equivalent in % 50.02
Decimal equivalent in % 50.03
num
3Decimal equivalent in % 50.05
Decimal equivalent in % 50.02
Decimal equivalent in % 50.03
num
7# define a number
2# define a number
3# define a number
4đầu ra
Converting number to percentage rounding to 0 place of decimal
Python3
Converting number to percentage rounding to 0 place of decimal 33% Converting number to percentage rounding to 2 place of decimal -50.00%1
Converting number to percentage rounding to 0 place of decimal 33% Converting number to percentage rounding to 2 place of decimal -50.00%2_______84_______
"{:.n%}".format[num]2
Decimal equivalent in % 50.02
Decimal equivalent in % 50.03
Converting number to percentage rounding to 0 place of decimal 33% Converting number to percentage rounding to 2 place of decimal -50.00%7
Decimal equivalent in % 50.05
Decimal equivalent in % 50.02
Decimal equivalent in % 50.03
# define a number
1# define a number
2# define a number
3# define a number
4Decimal equivalent in % 33.333
# define a number
6
Converting number to percentage rounding to 0 place of decimal 33% Converting number to percentage rounding to 2 place of decimal -50.00%2_______84_______
# define a number
9num
0Decimal equivalent in % 50.02
Decimal equivalent in % 50.03
num
3Decimal equivalent in % 50.05
Decimal equivalent in % 50.02
Decimal equivalent in % 50.03
num
7# define a number
2# define a number
3# define a number
4200% Converting number to percentage rounding to 0 place of decimal -700.00000%
Khi chuyển đổi số nguyên, số chữ số thập phân không đóng bất kỳ vai trò nào, vì số này được nhân trực tiếp với 100. Số lượng số 0 được thêm vào số tương đương với số lượng vị trí thập phân được chọn.