Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Persian and Arabic digits of a string to English using JavaScript?

How can I convert Persian/Arabic numbers to English numbers with a simple function?

arabicNumbers = ["١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩", "٠"] persianNumbers = ["۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹", "۰"] 

It is the same schema, but the code pages are different.

like image 945
Ali.MD Avatar asked Jul 15 '15 19:07

Ali.MD


People also ask

Are Persian and Arabic numbers the same?

The final way to easily tell Persian from Arabic and Kurdish is through numbers. Whereas Arabic and Kurdish are identical, Persian numbers four, five, and six are written differently: ۶ ۵ ۴. And remember that longer numbers are written left-to-right the same as in English, so that 10 appears as ١٠ and not as ٠١.


2 Answers

Use this simple function to convert your string

var persianNumbers = [/۰/g, /۱/g, /۲/g, /۳/g, /۴/g, /۵/g, /۶/g, /۷/g, /۸/g, /۹/g], arabicNumbers  = [/٠/g, /١/g, /٢/g, /٣/g, /٤/g, /٥/g, /٦/g, /٧/g, /٨/g, /٩/g], fixNumbers = function (str) {   if(typeof str === 'string')   {     for(var i=0; i<10; i++)     {       str = str.replace(persianNumbers[i], i).replace(arabicNumbers[i], i);     }   }   return str; }; 

Be careful, in this code the persian numbers codepage are different with the arabian numbers.

Example

var mystr = 'Sample text ۱۱۱۵۱ and ٢٨٢٢'; mystr = fixNumbers(mystr); 

Refrence

like image 88
Ali.MD Avatar answered Sep 21 '22 14:09

Ali.MD


Oneliner of all 6 possible translations between English, Arabic, and persian Digits.

const e2p = s => s.replace(/\d/g, d => '۰۱۲۳۴۵۶۷۸۹'[d]) const e2a = s => s.replace(/\d/g, d => '٠١٢٣٤٥٦٧٨٩'[d])  const p2e = s => s.replace(/[۰-۹]/g, d => '۰۱۲۳۴۵۶۷۸۹'.indexOf(d)) const a2e = s => s.replace(/[٠-٩]/g, d => '٠١٢٣٤٥٦٧٨٩'.indexOf(d))  const p2a = s => s.replace(/[۰-۹]/g, d => '٠١٢٣٤٥٦٧٨٩'['۰۱۲۳۴۵۶۷۸۹'.indexOf(d)]) const a2p = s => s.replace(/[٠-٩]/g, d => '۰۱۲۳۴۵۶۷۸۹'['٠١٢٣٤٥٦٧٨٩'.indexOf(d)])  e2p("asdf1234") // asdf۱۲۳۴ e2a("asdf1234") // asdf١٢٣٤ p2e("asdf۱۲۳۴") // asdf1234 a2e("asdf١٢٣٤") // asdf1234 p2a("asdf۱۲۳۴") // asdf١٢٣٤ a2p("asdf١٢٣٤") // asdf۱۲۳۴ 

Explaination:

  • (s => f(s))(x) is a lambda function that is immediately executed, and will be equal to f(x)
  • s.replace(pattern, function) looks for matches of pattern in s, for every match m it will replace m with function(m) in the string.
  • /\d/g is a regex pattern, \d means a digit in the English language, g means global. If you don't specify the g it will only match the first occurrence, otherwise it will match all the occurrences.
  • In this case for every English digit d in the string, that digit will be replaced by '۰۱۲۳۴۵۶۷۸۹'[d] so, 3 will be replaced by the third index in that list('۰۱۲۳۴۵۶۷۸۹') which is '۳'
  • /[۰-۹]/g is the equivalent regex for Persian digits this time we can't use the same method, before we took advantage of the fact that javascript is dynamically typed and that d is automatically converted from a string(regex match) to a number(array index) (you can do '1234'['1'] in javascript which is the same as '1234'[1])
  • but this time we can't do that because '1234'['۱'] is invalid. so we use a trick here and use indexOf which is a function that tells us the index of an element in an array(here a character in a string) so, '۰۱۲۳۴۵۶۷۸۹'.indexOf(۳) will give us 3 because '۳' is the third index in the string '۰۱۲۳۴۵۶۷۸۹'
like image 32
yukashima huksay Avatar answered Sep 18 '22 14:09

yukashima huksay