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

For example, the standard division symbol '/' rounds to zero:

```
>>> 4 / 100
0
```

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

Asked by:

**Freddie459**| Posted: 24-09-2021

# Answer 1

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 ΤΖΩΤΖΙΟΥ!

**Ryan332**| Posted: 25-10-2021

# Answer 2

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.

**Anna561**| Posted: 25-10-2021

# Answer 3

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
```

Answered by: **Brooke733**| Posted: 25-10-2021

# Answer 4

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

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

Answered by: **Dexter257**| Posted: 25-10-2021

# Answer 5

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
```

Answered by: **Andrew819**| Posted: 25-10-2021

# Answer 6

A simple route 4 / 100.0

or

4.0 / 100

Answered by:**Lucas783**| Posted: 25-10-2021

# Answer 7

Here we have two possible cases given below

```
from __future__ import division
print(4/100)
print(4//100)
```

Answered by: **Melissa311**| Posted: 25-10-2021

# Answer 8

Try 4.0/100

Answered by:**Ada923**| Posted: 25-10-2021

# Answer 9

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.

Answered by:**Maddie197**| Posted: 25-10-2021

# Answer 10

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.

Answered by:**Clark195**| Posted: 25-10-2021

# Answer 11

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

4.0/100.0

Answered by:**Patrick334**| Posted: 25-10-2021

# Answer 12

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

Answered by:**Daniel964**| Posted: 25-10-2021

# Answer 13

Import division from future library like this:

```
from__future__ import division
```

Answered by: **Kellan203**| Posted: 25-10-2021

# Similar questions

Python division operator acting strange when operands are negative

I accidentally stumbled upon a strange behavior in python.
Typing this peace of code in repl.
In [29]: 7 /-3
Out[29]: -3
Can find nowhere any reasonably explanation for this result.
What is happening here ?

python - python3 division operator exactly like in python2

I (really, really) need division operator in Python3 behaves like in Python2.
Python2 code:
--> 11/5
2
--> 11.0/5
2.2
But in Python3 we have
--> 11//5
2
--> 11.0//5
2.0
I can change / to // or whatever, but I expect the same results.
Any ideas?

Python 2: Why is floor division operator faster than normal division operator?

Consider the following Python 2 code
from timeit import default_timer
def floor():
for _ in xrange(10**7):
1 * 12 // 39 * 2 // 39 * 23 - 234
def normal():
for _ in xrange(10**7):
1 * 12 / 39 * 2 / 39 * 23 - 234
t1 = default_timer()
floor()
t2 = default_timer()
normal()
t3 = default_timer()
print 'Floor %.3f' % (t2 - t1)
print 'Normal %.3f' % (t3 - t2)
And the o...

Python division operator gives different results

In Python I am trying to divide an integer by half and I came across two different results based on the sign of the number.
Example:
5/2 gives 2
and
-5/2 gives -3
How to get -2 when I divide -5/2 ?

python - Division & Modulo Operator

I just started learning how to code, and I've been assigned a problem that I've been stuck on for many hours now and was hoping I could receive some hints at the very least to solve the problem. The main point of this exercise is to practice division and modulus. We can use basic statements, but nothing fancy like conditionals or anything since we haven't gotten to that point.
I need a user to input a # from 1 - 2...

Why does python floor division operator behave like this?

This question already has answers here:

regex - Find all uses of division operator in python code

I want to find all of the instances in my python code in which the division operator / is used. My first instinct is to use a regular expression. The expression needs to filter out non-division uses of /, i.e. path names. The best I've come up with is [ A-z0-9_\)]/[ A-z0-9_\(]. This would find the division operator in
foo/bar
foo / bar
foo/(bar*baz)
foo / ...

python - Divison without division operator

I was trying to solve this question from leetcode:
Given two integers dividend and divisor, divide two integers without
using multiplication, division, and mod operator. The integer division
should truncate toward zero
My solution:
def divide(dividend, divisor):
pseudo_count = 0 # pseudo-count
flag = False # True if divisor or dividend is negative
if divid...

Python 3.1 inline division override

I don't know if this is a bug in 3.1, but if I remember correctly "inline" division worked like this in pre-3k versions:
Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
... def __init__(self, x):
... self.x = x
... def __idiv__(self, y):
... self.x /...

Sum and Division example (Python)

>>> sum((1, 2, 3, 4, 5, 6, 7))
28
>>> 28/7
4.0
>>> sum((1,2,3,4,5,6,7,8,9,10,11,12,13,14))
105
>>> 105/7
15.0
>>>
How do I automate this sum and division using a loop maybe?
Edit: Maybe I wasn't clear - I want a loop to keep doing the sum (of multiples of 7, eg 1-7, 1-14, 1-21 etc..) until it reaches x (x is the user input)
Okay, figured...

python tuple division

TypeError: unsupported operand type(s) for /: 'tuple' and 'tuple'
I'm getting above error , while I fetched a record using query "select max(rowid) from table"
and assigned it to variable and while performing / operation is throws above message.
How to resolve this.

String Division in Python

I have a list of strings that all follow a format of parts of the name divided by underscores. Here is the format:
string="somethingX_somethingY_one_two"
What I want to know how to do it extract "one_two" from each string in the list and rebuild the list so that each entry only has "somethingX_somethingY". I know that in C, there is a strtok function that is useful for splitting in...

python - Division by Zero Errors

I have a problem with this question from my professor. Here is the question:
Write the definition of a function typing_speed , that receives two parameters. The first is the number of words that a person has typed (an int greater than or equal to zero) in a particular time interval. The second is the length of the time interval in seconds (an int greater than zero). The function returns the typing speed of that ...

python - Round with integer division

Is there is a simple, pythonic way of rounding to the nearest whole number without using floating point? I'd like to do the following but with integer arithmetic:
skip = int(round(1.0 * total / surplus))
==============
@John: Floating point is not reproducible across platforms. If you want your code to pass tests across different platforms then you need to avoid floating point (o...

Python: division error

Heres my method:
def geometricSum(commonRatio, firstTerm, lastTerm):
return ((firstTerm - lastTerm) * commonRatio) / (1 - commonRatio)
Interpreter testing:
>>> geometricSum(1.0,1.0,100.0)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "F:\PortablePython_1.1_py3.0.1\sumOfGeometric.py", li...

c# - Division by zero: int vs. float

Dividing an int by zero, will throw an exception, but a float won't - at least in Java. Why does a float have additional NaN info, while an int type doesn't?

python - Rounded division by power of 2

I'm implementing a quantization algorithm from a textbook. I'm at a point where things pretty much work, except I get off-by-one errors when rounding. This is what the textbook has to say about that:
Rounded division by 2^p may be carried out by adding an offset and right-shifting by p bit positions
Now, I get the bit about the right shift, but what offset are the...

python - Left Matrix Division and Numpy Solve

I am trying to convert code that contains the \ operator from Matlab (Octave) to Python. Sample code
B = [2;4]
b = [4;4]
B \ b
This works and produces 1.2 as an answer. Using this web page
http://mathesaurus.sourceforge.net/matlab-numpy.html
I tr...

# Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python