How do i convert a float to a percent in python?

I would like to convert a decimal number (say 0.33333) to percentage (expected answer 33.33%)

I used the following

x = 0.3333

print(format(x,'.2%'))

which gives indeed 33.33%

However, the result is a string, I would like it to still be a number (in % format) to be able to perform mathematical operations on it (e.g. format(x,'.2%')*2 to give 66.66%

But this throws an exception as 33.33% is a string

How do i convert a float to a percent in python?

rdas

19.4k6 gold badges32 silver badges43 bronze badges

asked Jun 1, 2019 at 21:49

4

An idea is to create a custom datatype, I'll call it Percent, which inherit everything from float, but when you want to use it as a string it shows a % and multiplies the number with 100.

class Percent(float):
    def __str__(self):
        return '{:.2%}'.format(self)
x = Percent(0.3333)
print(x)
# 33.33%

If you want to represnt the value in envoirment like jupyter notebook in your format you can add same method with the name __repr__ to Percent Class:

def __repr__(self):
    return '{:.2%}'.format(self)

user35443

6,14111 gold badges52 silver badges72 bronze badges

answered Jun 1, 2019 at 22:31

How do i convert a float to a percent in python?

MehdiMehdi

1,1521 gold badge14 silver badges34 bronze badges

I made it on a single line like:

x = 0.333
print(str(int(x*100)) + "%)

answered Feb 12, 2021 at 22:36

1

x = 0.3333

print('{:,.2%}'.format(x))  # Prints percentage format, retains x as float variable

Outputs 33.33%

answered Apr 23, 2021 at 23:09

Use f-strings! (python >= 3.6)

x = 0.3333

print(f"{x:,.2%}")

That is: print x, to 2 decimal places, and add the % symbol at the end. x is not altered by the f-string.

answered Jun 5 at 2:49

vtnatevtnate

1211 silver badge8 bronze badges

Converting from float to percentage

I would like to convert a decimal number (say 0.33333) to percentage (expected answer 33.33%)

I used the following

x = 0.3333

print(format(x,'.2%'))

which gives indeed 33.33%

However, the result is a string, I would like it to still be a number (in % format) to be able to perform mathematical operations on it (e.g. format(x,'.2%')*2 to give 66.66%

But this throws an exception as 33.33% is a string

Answer

An idea is to create a custom datatype, I’ll call it Percent, which inherit everything from float, but when you want to use it as a string it shows a % and multiplies the number with 100.

class Percent(float):
    def __str__(self):
        return '{:.2%}'.format(self)
x = Percent(0.3333)
print(x)
# 33.33%

If you want to represnt the value in envoirment like jupyter notebook in your format you can add same method with the name __repr__ to Percent Class:

def __repr__(self):
    return '{:.2%}'.format(self)

How do I convert a value to a percent in Python?

To print a percentage value in Python, use the str. format() method or an f-string on the format language pattern "{:..
your_number = 0.42..
percentage = "{:. 0%}". format(your_number).
print(percentage).

Is there a percentage function in Python?

In python the “%” has two different uses. First, when used in a math problem like those above, it is used to represent modulus. The modulus of a number is the remainder left when you divide. 4 mod 5 = 4 (you can divide 5 into 4 zero times with a remainder of 4).

What is the percent symbol in Python?

In python, the percent sign is called modulo operator ” % “ which returns the remainder after dividing the left-hand operand by right-hand operand.