[Django]-How to convert normal numbers into arabic numbers in django

4👍

try this function:

def enToArNumb(number): 
    dic = { 
        0:'۰', 
        1:'١', 
        2:'٢', 
        3:'۳', 
        4:'۴', 
        5:'۵', 
        6:'۶', 
        7:'۷', 
        8:'۸', 
        9:'۹', 
        .:'۰', 
    }
    return dic.get(number)

and use it like this:

ar_numbers = [enToArNumb(num) for num in numbers]

3👍

Try this:

ar_num = '۰١٢٣٤٥٦٧٨٩'
en_num = '0123456789'

table = str.maketrans(en_num, ar_num)

normalized = "643256284534".translate(table)
print(normalized)

The output:

٦٤٣٢٥٦٢٨٤٥٣٤

2👍

To convert a (unicode) string of digits to an equivalent “Arabic-Indic” digit you can use unicode.translate():

arabic_indic_trans = dict(
    zip((ord(s) for s in u'0123456789'),
        u'\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669')
)

>>> for s in u'0123456789', u'33342353', u'88192838743':
...     print('{!r} -> {!r}'.format(s, s.translate(arabic_indic_trans)))
u'0123456789' -> u'\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669'
u'33342353' -> u'\u0663\u0663\u0663\u0664\u0662\u0663\u0665\u0663'
u'88192838743' -> u'\u0668\u0668\u0661\u0669\u0662\u0668\u0663\u0668\u0667\u0664\u0663'

This should work in Python 2 and 3.

To convert from an integer, first convert the integer to a unicode string, then translate:

>>> for s in 1234567890, 33342353, 88192838743876487602873545683470:
...     s = unicode(s)    # Python 2 (s = str(s) for Python 3)
...     print('{!r} -> {!r}'.format(s, s.translate(arabic_indic_trans)))
👤mhawke

0👍

This is how the translation is done for Middle East region, since Hassan’s answer uses a numerals that are close to the standard Arabic numerals but not the same.

def en_to_ar_num(number_string):
    dic = {
        '0': '۰',
        '1': '١',
        '2': '٢',
        '3': '۳',
        '4': '٤',
        '5': '۵',
        '6': '٦',
        '7': '۷',
        '8': '۸',
        '9': '۹',
    }

    return "".join([dic[char] for char in number_string])

en_to_ar_num("124") # ١٢٤

0👍

According to shakram02 answer you can do this to translate English number to Arabic one if they have dash or in string:

def en_to_ar_num(number_string):
   lis=[]
   dic = {
          '0': '۰',
          '1': '١',
          '2': '٢',
          '3': '۳',
          '4': '٤',
          '5': '۵',
          '6': '٦',
          '7': '۷',
          '8': '۸',
          '9': '۹',
   }

   for char in number_string:
          if char in dic:
                 lis.append(dic[char])
          else:
                 lis.append(char)
   return "".join(lis)

Print:

print(en_to_ar_num("124-25-abc"))

Output:
i screen it cause it wasn’t appear like this in my post

👤5no0p

Leave a comment