How do I get a decimal value when using the division operator in Python?

Each Answer to this Q is separated by one/two green lines.

For example, the standard division symbol “https://stackoverflow.com/” rounds to zero:

>>> 4 / 100
0

However, I want it to return 0.04. What do I use?

There are three options:

>>> 4 / float(100)
0.04
>>> 4 / 100.0
0.04

which is the same behavior as the C, C++, Java etc, or

>>> from __future__ import division
>>> 4 / 100
0.04

You can also activate this behavior by passing the argument -Qnew to the Python interpreter:

$ python -Qnew
>>> 4 / 100
0.04

The second option will be the default in Python 3.0. If you want to have the old integer division, you have to use the // operator.

Edit: added section about -Qnew, thanks to ????????!

Other answers suggest how to get a floating-point value. While this wlil be close to what you want, it won’t be exact:

>>> 0.4/100.
0.0040000000000000001

If you actually want a decimal value, do this:

>>> import decimal
>>> decimal.Decimal('4') / decimal.Decimal('100')
Decimal("0.04")

That will give you an object that properly knows that 4 / 100 in base 10 is “0.04”. Floating-point numbers are actually in base 2, i.e. binary, not decimal.

Make one or both of the terms a floating point number, like so:

4.0/100.0

Alternatively, turn on the feature that will be default in Python 3.0, ‘true division’, that does what you want. At the top of your module or script, do:

from __future__ import division

You might want to look at Python’s decimal package, also. This will provide nice decimal results.

>>> decimal.Decimal('4')/100
Decimal("0.04")

You need to tell Python to use floating point values, not integers. You can do that simply by using a decimal point yourself in the inputs:

>>> 4/100.0
0.040000000000000001

A simple route 4 / 100.0

or

4.0 / 100

Here we have two possible cases given below

from __future__ import division

print(4/100)
print(4//100)

Try 4.0/100

You cant get a decimal value by dividing one integer with another, you’ll allways get an integer that way (result truncated to integer). You need at least one value to be a decimal number.

Add the following function in your code with its callback.

# Starting of the function
def divide(number_one, number_two, decimal_place = 4):
    quotient = number_one/number_two
    remainder = number_one % number_two
    if remainder != 0:
        quotient_str = str(quotient)
        for loop in range(0, decimal_place):
            if loop == 0:
                quotient_str += "."
            surplus_quotient = (remainder * 10) / number_two
            quotient_str += str(surplus_quotient)
            remainder = (remainder * 10) % number_two
            if remainder == 0:
                break
        return float(quotient_str)
    else:
        return quotient
#Ending of the function

# Calling back the above function
# Structure : divide(<divident>, <divisor>, <decimal place(optional)>)
divide(1, 7, 10) # Output : 0.1428571428
# OR
divide(1, 7) # Output : 0.1428

This function works on the basis of “Euclid Division Algorithm”. This function is very useful if you don’t want to import any external header files in your project.

Syntex : divide([divident], [divisor], [decimal place(optional))

Code : divide(1, 7, 10) OR divide(1, 7)

Comment below for any queries.

You could also try adding a “.0” at the end of the number.

4.0/100.0

It’s only dropping the fractional part after decimal.
Have you tried : 4.0 / 100

Import division from future library like this:

from__future__ import division


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .