oTree Forum >

Currency - add comma and remove space

#1 by Inbal

Hi,
I am planning to run an experiment in India. I changed the currency unit to INR, but now whenever I use a currency field, there appears a space between the coin symbol and the amount of money (which didn't appear when I used other currencies). Is there a way to remove that space? Also, is there a way to add commas to currency fields when referring to large numbers?
I.e., I want the output to look like, e.g., ₹10,000 instead of ₹ 10000.
Best,
Inbal

#2 by BonnEconLab

Hi Inbal,

There are multiple ways to manipulate strings via JavaScript. I think it is safer, though, to do the manipulation via Python. In oTree, use vars_for_template for this purpose. It seems that you are using a currency field. You may have to convert this to a “regular” floating-point number first.

For instance, based on the guess_two_thirds example game,

# PAGES

class Introduction(Page):

    ...
    @staticmethod
    def vars_for_template(player):
        jackpot_float = float(C.JACKPOT)
        return dict(
            jackpot_formatted = f'₹{jackpot_float:,.2f}',
            jackpot_formatted_older_python = '₹{:,.2f}'.format(jackpot_float),
        )

See also https://stackoverflow.com/questions/1823058/how-to-print-a-number-using-commas-as-thousands-separators.

Best,

Holger

#3 by Inbal

Hi Holger,
Thank you! This is very helpful!
I'm curious if there's a way to also permanently remove the space in the currency field for the INR currency unit. I didn't notice any spaces when using other currency units.
Best,
Inbal

#4 by BonnEconLab

Shalom Inbal,

I guess that there is a way ... But you have to edit oTree’s core files.

There should be a file .../site-packages/otree/i18n.py (at least there is with the current version, 5.10.3). The path (...) will depend on how exactly you installed oTree (e.g., whether in a virtual environment or not).

In that file, search for “INR”. You will find

if lc == 'en':
        if CUR in ['USD', 'CAD', 'AUD']:
            return '$#'
        if CUR == 'GBP':
            return '£#'
        if CUR == 'EUR':
            return '€#'
        if CUR == 'INR':
            return '₹ #'

Removing the space between “₹” and “#” should do the job ... But I haven’t tested this.

The space definitely isn’t present on the Indian Rupee banknotes (https://upload.wikimedia.org/wikipedia/commons/b/b8/Banknote_of_india.png).

Cheers,

Holger

#5 by Inbal

Thank you very much, Holger!
Best,
Inbal

#6 by BonnEconLab

You’re welcome. A caveat when changing the oTree-wide i18n.py file is that you do not only need to change it locally but also on the server that you will use to run your study.

I have tried to overwrite settings from i18n.py and currency.py by redefining the respective methods in the __init__.py ... but I failed.

To keep your code portable, I would therefore suggest to use a workaround. Based on the number formatting that I suggested earlier, you can, for instance, define a function

def INR(num) -> str:
    if num >= 0:
        return '₹{:,.2f}'.format(num)
    else:
        return '−₹{:,.2f}'.format(-num)

This also uses the typographically correct minus symbol (https://unicode-explorer.com/c/2212) instead of the hyphen for negative values.

This allows you to use something like

# PAGES

class Introduction(Page):

    ...
    @staticmethod
    def vars_for_template(player):
        return dict(
            jackpot_INR = INR(C.JACKPOT),
        )

#7 by Inbal

Thanks a lot!
Best,
Inbal

Write a reply

Set forum username