Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between quantize() and str.format()?

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

like image 325
orokusaki Avatar asked Mar 02 '26 08:03

orokusaki


1 Answers

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.

like image 86
Seth Avatar answered Mar 03 '26 21:03

Seth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!