What is the difference between quantization () and str.format ()?

I do not mean what the technical difference is, but rather what is 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 quantization when

+3
source share
1 answer

Decimal.quantizereturns a new Decimalone that has a different value.

''.format() formats a string.

In this particular case, printing the result gives the same result. In addition, they are completely different operations that return completely different types.

+7
source

Source: https://habr.com/ru/post/1727524/


All Articles