django-shop icon indicating copy to clipboard operation
django-shop copied to clipboard

It seems DECIMAL_PLACES is no effect

Open vivazzi opened this issue 7 years ago • 12 comments

I find Its description in settings.py only. And one question: how I can display unit_price without zero after comma in template. For example:

{{ product.unit_price }}

I wan't see $ 12 (not $ 12.00).

Now I use own custom templatetags for fix it.

vivazzi avatar Nov 17 '16 07:11 vivazzi

This is hard coded in shop.money.iso4217.py for each currency according to it's decimal places.

Hmm, didn't think about the use-case for selling real estate :/

jrief avatar Nov 17 '16 07:11 jrief

In our country it seldom used dime (or cents for USA) in web-shops. Also I'd like to separate big values by space. For example, $ 25050 -> $ 25 000. And I have my workaround for it too. I'd like use shop's functions for it.

vivazzi avatar Nov 17 '16 07:11 vivazzi

Ok, then that mapping should be configurable

jrief avatar Nov 17 '16 08:11 jrief

It would be good! Say, SHOP_DECIMAL_SEPARATED_PLACES = True by default.

vivazzi avatar Nov 17 '16 08:11 vivazzi

Isn't it better to just write a template filter?

rfleschenberg avatar Nov 17 '16 11:11 rfleschenberg

Yes, now I use my custom filter:

@register.filter
def price_format(value, use_currency_title=True):
    if not value:
        return value

    if type(value) == unicode:
        value = Decimal(value.replace(',', '.'))

    if settings.SHOP_DECIMAL_PLACES == 0: precision = '1'
    else: precision = '0.{}1'.format('0' * (settings.SHOP_DECIMAL_PLACES - 1))

    price = separate_by_space(value.quantize(Decimal(precision)))

    if not use_currency_title:
        return price

    return settings.SHOP_MONEY_FORMAT.format(symbol=settings.SHOP_DEFAULT_CURRENCY_TITLE, amount=price)


@register.filter
def separate_by_space(value):
    result = ''
    parts = unicode(value).split()
    for part in parts:
        try:
            n = decimal.Decimal(part)
            result += '{0:,} '.format(n).replace(',', ' ')
        except decimal.InvalidOperation:
            result += '{} '.format(part)
    return result.replace('  ', ' ').strip()

Maybe Django Shop is needed like such price_format filter? SHOP_DEFAULT_CURRENCY_TITLE is $ or and etc.

vivazzi avatar Nov 22 '16 12:11 vivazzi

I'll leave this open for now, until I find a cleaner or built-in solution.

Ann.: One of the reasons I introduced the Money class, was, not having to pre- or postfix rendered amounts. Therefore I would appreciate a more generic solution.

Feel free to remind me about this in the future.

jrief avatar Nov 22 '16 13:11 jrief

@vivazzi I think the best place to look how money is rendered is here. shop.money.money_maker.AbstractMoney.__format__.

USE_THOUSAND_SEPARATOR = True makes things even worse, because it alway adds a dot, so that an amount of 987 would be rendered as € .987,00.

So this issue describes two separate problems. On one side you want to configure the decimal places yourself. Would it make sense for you, if I add a hook where you can use your own version of money.currencies.CURRENCIES?

The second is what to use as the thousands separator. Could you please describe your use cases.

jrief avatar Nov 24 '16 22:11 jrief

I need think about it.. Where I can see USE_THOUSAND_SEPARATOR implementation? In Django Shop I don't found.

On one side you want to configure the decimal places yourself. Would it make sense for you, if I add a hook where you can use your own version of money.currencies.CURRENCIES?

Can we bind SHOP_DECIMAL_PLACES with money.currencies.CURRENCIES? Other words, can we add SHOP_DECIMAL_PLACES to money.currencies.CURRENCIES?

For example:

# in shop/settings.py:
DECIMAL_PLACES = getattr(settings, 'SHOP_DECIMAL_PLACES', None)

# in money.currencies.CURRENCIES
'RUB': ('643', DECIMAL_PLACES or 2, '₽', _("Russian Ruble")),

I didn't test it but It seems must work.

vivazzi avatar Nov 25 '16 12:11 vivazzi

Decimal places are a per currency configuration. There are currencies with 0 DP: Japanese Yen, Hungarian Forint; some with 2 DP: Pound, Euro, Dollar and even some with 3 DP: Omani rial, Kuwait dinar. Therefore I don't like the idea of a global decimal places setting.

Therefore I will add a configuration directive which can be used to replace the whole iso4217.CURRENCIES table.

jrief avatar Nov 25 '16 12:11 jrief

Yes, you are right. Your variant is better because our products can be in different currencies. It is better to use own configuration directive

vivazzi avatar Nov 25 '16 12:11 vivazzi

Where I can see USE_THOUSAND_SEPARATOR implementation?

This is part of Django itself, but currently it is not supported by django-SHOP.

jrief avatar Nov 25 '16 12:11 jrief