Kiểu dữ liệu cho tỷ lệ phần trăm trong python là gì?

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.33
0
Decimal equivalent in %
33.33
1
Decimal equivalent in %
33.33
2

Decimal equivalent in %
33.33
3

Decimal equivalent in %
33.33
4

Decimal equivalent in %
33.33
5_______84_______ num
Decimal equivalent in %
33.33
8
Decimal equivalent in %
33.33
9

Decimal equivalent in %
33.33
3

Decimal equivalent in %
50.0
1

Decimal equivalent in %
50.0
2
Decimal equivalent in %
50.0
3
Decimal equivalent in %
50.0
4
Decimal equivalent in %
50.0
5

Decimal equivalent in %
33.33
3

Decimal equivalent in %
50.0
7

Decimal equivalent in %
50.0
2
Decimal equivalent in %
50.0
3
"{:.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.33
1
"{:.n%}".format[num]
9

Decimal equivalent in %
33.33
3

Decimal equivalent in %
33.33
4

Decimal equivalent in %
33.33
5_______84_______ num
Decimal equivalent in %
33.33
8
Decimal equivalent in %
33.33
9

Decimal equivalent in %
33.33
3

Decimal equivalent in %
50.0
1

Decimal equivalent in %
50.0
2
Decimal equivalent in %
50.0
3
Decimal equivalent in %
50.0
4
Decimal equivalent in %
50.0
5

Decimal equivalent in %
33.33
3

Decimal equivalent in %
50.0
7

Decimal equivalent in %
50.0
2
Decimal equivalent in %
50.0
3
"{:.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.0
2
Decimal equivalent in %
50.0
3
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.0
5

Decimal equivalent in %
50.0
2
Decimal equivalent in %
50.0
3# define a number1# define a number2# define a number3# define a number4

Decimal equivalent in %
33.33
3

# define a number6

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 number9num0

Decimal equivalent in %
50.0
2
Decimal equivalent in %
50.0
3num3
Decimal equivalent in %
50.0
5

Decimal equivalent in %
50.0
2
Decimal equivalent in %
50.0
3num7# define a number2# define a number3# define a number4

đầ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.0
2
Decimal equivalent in %
50.0
3
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.0
5

Decimal equivalent in %
50.0
2
Decimal equivalent in %
50.0
3# define a number1# define a number2# define a number3# define a number4

Decimal equivalent in %
33.33
3

# define a number6

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 number9num0

Decimal equivalent in %
50.0
2
Decimal equivalent in %
50.0
3num3
Decimal equivalent in %
50.0
5

Decimal equivalent in %
50.0
2
Decimal equivalent in %
50.0
3num7# define a number2# define a number3# define a number4

200%
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.  

Chủ Đề