2

I'd like to pass numbers around between functions, while preserving the decimal places for the numbers.

I've discovered that if I pass a float like '10.00' in to a function, then the decimal places don't get used. This messes an operation like calculating percentages.

For example, x * (10 / 100) will always return 0. But if I manage to preserve the decimal places, I end up doing x * (10.00 / 100). This returns an accurate result.

I'd like to have a technique that enables consistency when I'm working with numbers that decimal places that can hold zeroes.

davedave
  • 603
  • 2
  • 12
  • 18
  • Also, I think you're wrong. If you pass a float, it will be used like a float. IMO you are passing an int there. – tayfun Jan 19 '14 at 21:04
  • As soon as there is one float involved Python 2 will do an float division, so you should explicitly specify the `.0` when calling the function. Python 3 always performs a float division unless explicitly told otherwise. – Cu3PO42 Jan 19 '14 at 21:06

5 Answers5

3

When you write

10 / 100

you are performing integer division. That's because both operands are integers. The result is 0.

If you want to perform floating point division, make one of the operands be a floating point value. For instance:

10.0 / 100

or

float(10) / 100

Do beware also that

10.0 / 100

results in a binary floating point value and binary floating data types cannot represent the true result value of 0.1. So if you want to represent the result accurately you may need to use a decimal data type. The decimal module has the functionality needed for that.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

Python 2.x will use integer division when dividing two integers unless you explicitly tell it to do otherwise. Two integers in --> one integer out.

Python 3 onwards will return, to quote PEP 238 http://www.python.org/dev/peps/pep-0238/ a reasonable approximation of the result of the division approximation, i.e. it will perform a floating point division and return the result without rounding.

To enable this behaviour in earlier version of Python you can use:

from __future__ import division

At the very top of the module, this should get you the consistent results you want.

James
  • 3,252
  • 1
  • 18
  • 33
2

You should use the decimal module. Each number knows how many significant digits it has.

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
2

Division in python for float and int works differently, take a look at this question and it's answers: Python division.

Moreover, if you are looking for a solution to format a decimal floating point of your figures into string, you might need to use %f.

Python

# '1.000000'
"%f" % (1.0)

# '1.00'
"%.2f" % (1.0)

# '  1.00'
"%6.2f" % (1.0)
Community
  • 1
  • 1
Mehdi
  • 4,202
  • 5
  • 20
  • 36
1

If you're trying to preserve significant digits, the decimal module is has everything you need. Example:

>>> from decimal import Decimal
>>> num = Decimal('10.00')
>>> num
Decimal('10.00')
>>> num / 10
Decimal('1.00')
mhlester
  • 22,781
  • 10
  • 52
  • 75
  • A more dramatic example: `str(Decimal(time.time()))` – scharfmn Aug 21 '15 at 19:24
  • @bahmait true, that's very interesting. The point I was trying to convey though was that it maintains a concept of significant digits. i.e if you enter `10.00` it distinguishes it from `10.0` or `10.000`. Any float converted to Decimal directly can give you a uselessly long result as you pointed out. Try `Decimal(0.1)`! – mhlester Aug 21 '15 at 19:29