3

I don't mean what's the technical difference, but rather, what's the faster/more logical or Pythonic, etc. way to do this:

    def __quantized_price(self):
        TWOPLACES = Decimal(10) ** -2
        return self.price.quantize(TWOPLACES)

or

    def __formatted_price(self):
        TWOPLACES = Decimal(10) ** -2
        return '{0:.2f}'.format(self.price)

They seem to be exactly the same so I'm just wondering why they created quantize when

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
orokusaki
  • 55,146
  • 59
  • 179
  • 257
  • Ummm... The only thing that matters is the technical difference, since they do completely different things. – S.Lott Jan 06 '10 at 19:56

1 Answers1

8

Decimal.quantize returns a new Decimal that has a different value.

''.format() formats a string.

In this particular case printing the result yields the same output. Other than that they are totally different operations returning totally different types.

Seth
  • 45,033
  • 10
  • 85
  • 120
  • Oh, so 5.00 is a different value than 5? – orokusaki Jan 06 '10 at 21:04
  • Yes, they are very different. Do you remember physics? A value of 5 with three significant digits (+/- 0.005) is not the same as a value of 5 with a single significant digit (+/- 0.5). – Marco Mariani Mar 19 '10 at 14:09