1

I have a calculation that calculates the tax when a user checks out using Cartridge as my shop framework.

tax = tax * Decimal(str(settings.SHOP_DEFAULT_TAX_RATE))

The calculation works properly.I then pass tax to set_tax(request, _("GST+PST"), tax)

When I do that, I get an error Decimal('12.0') is not JSON serializable.

When I convert it to a float (set_tax(request, _("GST+PST"), float(tax)) it works; but when I go through the checkout I get this error:

unsupported operand type(s) for +=: 'Decimal' and 'float'

In my settings:

  • SHOP_DEFAULT_TAX_RATE = 0.12

I've seen solutions where I need to use a custom JSONEncoder but the error occurs inside Django itself.

Thanks.

koralarts
  • 2,062
  • 4
  • 30
  • 48
  • 4
    related http://stackoverflow.com/q/1960516/674039 – wim Mar 13 '14 at 19:47
  • Out of curiosity, why do you need Decimal at all? If you're dealing with taxes (amounts of money), floating precision should be fine unless you're planning on handling ALOT of cash ... – mgilson Mar 13 '14 at 19:52
  • 3
    @mgilson: actually, the opposite is true; The problem is that money is not a *measurement* subject to instrument sensitivity; the notion of precision is absurd in financial calculations. `USD0.02` is *exactly* two cents; not two cents plus or minus half a cent. – SingleNegationElimination Mar 13 '14 at 20:05
  • @IfLoop: how do you distribute `$1` among three people? Remember you said that account must balance *exactly* – jfs Mar 14 '14 at 15:59
  • @J.F: another variation is "divide a 17 camel estate to 3 sons, one receiving half, one a third and one a ninth". A correct answer does not follow the form of "fill a jug to 3 gallons, you have empty 4 and 5 gallon jugs and an unlimited supply of water". The difference is that camel math is discreet but water is continuous. – SingleNegationElimination Mar 14 '14 at 19:36
  • @IfLoop: the point being that it might make sense to use `Decimal` to perform calculations with the high but finite precision (i.e., inexact). Note: `Decimal` along is not enough e.g., to compute a sum, `math.fsum` algorithm could be used instead of `sum`. – jfs Mar 14 '14 at 19:58

1 Answers1

3

This was fixed a couple weeks ago but has not yet been released:

https://github.com/stephenmcd/cartridge/commit/628bd203f39a62d9de2613de7057e6742657111f

Fortunately you can add the development version as a dependency right now.

BTW decimal is the correct type to be using.

Steve
  • 1,726
  • 10
  • 16