What's the difference between quantize() and str.format()?
-
19-09-2019 - |
Question
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
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow