1

I need to round values to exactly 2 decimals, but when I use round() it doesn't work, if the value is 0.4, but I need 0.40.

round(0.4232323, 2) = 0.42
bad: round(0.4, 2) = 0.4

How can I solve this?

Gray
  • 7,050
  • 2
  • 29
  • 52
dassau
  • 141
  • 2
  • 7

3 Answers3

7

0.4 and 0.40 are mathematically equivalent.

If you want to display them with two decimal places, use {:.2f} formatting:

>>> '{:.2f}'.format(0.4)
'0.40'
nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Are you sure about that? I would say they different precisions. – Joe Oct 01 '13 at 15:10
  • @Joe: They are in fact **the exact same number**. Python doesn't care about precision or significant figures. – nneonneo Oct 01 '13 at 15:10
  • @Joe: Sure about what? .4 and .40 are exactly equal. The result of subtracting one from the other is exactly zero. “.4” and “.40” are just different numerals for the same number. – Eric Postpischil Oct 01 '13 at 15:11
  • `>>> from decimal import Decimal >>> a = Decimal("0.40") >>> b = Decimal("0.4") >>> a == b True >>> a + 1 Decimal('1.40') >>> b + 1 Decimal('1.4')` – Joe Oct 01 '13 at 15:11
  • They equal each other but they can be represented with different precisions. – Joe Oct 01 '13 at 15:12
  • @Joe: That's the `Decimal` module, which is an entirely separate beast. `float` doesn't carry around precisions. – nneonneo Oct 01 '13 at 15:12
  • The question is talking about decimal places. It seems like a good idea to mention the module designed to solve that problem. – Joe Oct 01 '13 at 15:12
  • @Joe: “.4 with one digit of precision” is certainly a different thing from “.40 with two digits of precision”. But .4 and .40 are exactly the same number. The source code texts `Decimal("0.40")` and `Decimal("0.4")` may result in different values. But those are different things from .4 or .40. When working with computers, you **must** be precise with terminology. If you want to talk about an object which carries both a value and some notion of precision, then you must say so. Simply referring to .4 or .40 is referring to numbers. – Eric Postpischil Oct 01 '13 at 15:14
  • @EricPostpischil Good idea. I've given an answer. As per the new comment, it appears that the question was about formatting after all. But worth covering all bases, I think. – Joe Oct 01 '13 at 15:18
1
print("{0:.2f}".format(round(0.4232323, 2)))
1

If you represent these values as floats then there is no difference between 0.4 and 0.40. To print them with different precision is just a question of format strings (as per the other two answers).

However, if you want to work with decimals, there is a decimal module in Python.

>>> from decimal import Decimal
>>> a = Decimal("0.40")
>>> b = Decimal("0.4")
# They have equal values
>>> a == b
True 

# But maintain their precision
>>> a + 1
Decimal('1.40')

>>> b + 1
Decimal('1.4') 

>>> a - b
Decimal('0.00')

Use the quantize method to round to a particular number of places. For example:

>>> c = Decimal(0.4232323)
>>> c.quantize(Decimal("0.00"))
Decimal('0.42')
>>> str(c.quantize(Decimal("0.00")))
'0.42'
Joe
  • 46,419
  • 33
  • 155
  • 245