Alex Rivera | Logout

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

Asked 2008-09-22T20:06:16.723
74

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

>>> 4 / 100
0

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

Edit
Report

2 Answers

6

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 2008-09-22T21:38:16.113
2

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 2008-09-22T20:11:02.360

Your Answer