📜  如何在Python中将分数转换为百分比?

📅  最后修改于: 2022-05-13 01:54:18.900000             🧑  作者: Mango

如何在Python中将分数转换为百分比?

分数是整个商品的一部分,可以用分子除以分母的形式表示。一个分数的形式是\pm\frac{p}{q}    ,其中 p<=q。

有多种方法可以在分数和百分比之间进行转换。

方法一:

可以通过将有理数乘以 100 来手动将分数转换为百分比。 可以使用内置的 math.round()函数将获得的十进制数四舍五入到所需的位数,该函数具有以下语法:

round(number, digits)
Python3
# define a number
num = 1/3
  
# converting to decimal value
dec_num = num * 100
  
# rounding number to required number of decimal places 
print ("Decimal equivalent in %")
  
# round to 2 places
print (round(dec_num,2))


Python3
# define a number
num = 2/4
  
# converting to decimal value
dec_num = num * 100
  
# rounding number to required number of decimal places 
print ("Decimal equivalent in %")
  
# round to 2 places
print (round(dec_num,4))


Python3
# input an integer with denominator =1 
integer_num = 2 
print ("Converting number to percentage rounding to 0 place of decimal")
print ("{:.0%}".format(integer_num))
  
# input an negative integer with denominator =1 
integer_num = -7 
print ("Converting number to percentage rounding to 5 place of decimal")
print ("{:.5%}".format(integer_num))


Python3
# input an integer with denominator =1 
integer_num = 2 
print ("Converting number to percentage rounding to 0 place of decimal")
print ("{:.0%}".format(integer_num))
  
# input an negative integer with denominator =1 
integer_num = -7 
print ("Converting number to percentage rounding to 5 place of decimal")
print ("{:.5%}".format(integer_num))


Python3
# input a fractional number
num = 1/3
  
# taking 0 places to decimal and convert it to decimal
print ("Converting number to percentage rounding to 0 place of decimal")
print ("{:.0%}".format(num))
num = -2/4
  
# taking 2 places to decimal and convert it to decimal
print ("Converting number to percentage rounding to 2 place of decimal")
print ("{:.2%}".format(num))


输出

Decimal equivalent in %
33.33

如果分子绝对可以被分母整除,则数字返回到小数点后 1 位。

蟒蛇3

# define a number
num = 2/4
  
# converting to decimal value
dec_num = num * 100
  
# rounding number to required number of decimal places 
print ("Decimal equivalent in %")
  
# round to 2 places
print (round(dec_num,4))

输出

Decimal equivalent in %
50.0

方法二:

str.format() 方法用于通过指定小数点后的位数将数字转换为百分比。

"{:.n%}".format(num)
  • n代表小数点后的位数
  • num 表示以小数或分数表示的浮点数

分数的分母为 1 的特殊情况,即小数变为等价于整数值。

蟒蛇3

# input an integer with denominator =1 
integer_num = 2 
print ("Converting number to percentage rounding to 0 place of decimal")
print ("{:.0%}".format(integer_num))
  
# input an negative integer with denominator =1 
integer_num = -7 
print ("Converting number to percentage rounding to 5 place of decimal")
print ("{:.5%}".format(integer_num))

输出

Converting number to percentage rounding to 0 place of decimal

蟒蛇3

# input an integer with denominator =1 
integer_num = 2 
print ("Converting number to percentage rounding to 0 place of decimal")
print ("{:.0%}".format(integer_num))
  
# input an negative integer with denominator =1 
integer_num = -7 
print ("Converting number to percentage rounding to 5 place of decimal")
print ("{:.5%}".format(integer_num))
200%
Converting number to percentage rounding to 0 place of decimal
-700.00000%

在转换整数时,小数位数没有任何作用,因为数字是直接乘以 100 的。数字后附加的零的数量等于选择的小数位数。

以下Python代码用于将有理数转换为等效百分比:

蟒蛇3

# input a fractional number
num = 1/3
  
# taking 0 places to decimal and convert it to decimal
print ("Converting number to percentage rounding to 0 place of decimal")
print ("{:.0%}".format(num))
num = -2/4
  
# taking 2 places to decimal and convert it to decimal
print ("Converting number to percentage rounding to 2 place of decimal")
print ("{:.2%}".format(num))

输出

Converting number to percentage rounding to 0 place of decimal
33%
Converting number to percentage rounding to 2 place of decimal
-50.00%