1

I have a function in which I'm converting an input string (arabic numeral) into an int value. I do this with the following function.

function convertToEnglishDigit(input) {
    var numberMap = {
        '۰': '0',
        '۱': '1',
        '۲': '2',
        '۳': '3',
        '۴': '4',
        '۵': '5',
        '۶': '6',
        '۷': '7',
        '۸': '8',
        '۹': '9',        
    };
    var result = parseInt(input.replace(/[۰-۹]/g, function(i) {
       return numberMap[i];
    }));

    return result;
} 

It works if the input is either a single character, such as '۲' or if the string of characters are sequential such as '۱۲۳۴۵'. If the input is a string of characters which are out of order such as "٢١٠" the returned value of the replace() is NaN, why is this and how can I account for the situation in which they are out of order, which in my use case, will be almost always.

Alon Adler
  • 3,984
  • 4
  • 32
  • 44
Stavros_S
  • 2,145
  • 7
  • 31
  • 75

2 Answers2

1

For whatever reason, the regex is very confused by those literal characters. You can make it work by using the hex codes for those digits:

function convertToEnglishDigit(input) {
    var numberMap = {
      '\u0660': '0',
      '\u0661': '1',
      '\u0662': '2',
      '\u0663': '3',
      '\u0664': '4',
      '\u0665': '5',
      '\u0666': '6',
      '\u0667': '7',
      '\u0668': '8',
      '\u0669': '9'        
    };
    var result = parseInt(input.replace(/[\u0660-\u0669]/g, function(i) {
       return numberMap[i];
    }));

    return result;
}
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Good Find! I wonder if I even need to map the values in the object. Seems just using the number strings and the hex codes in the replace would be enough. – Stavros_S Dec 01 '16 at 21:14
  • any idea how I could handle floating point numbers with this method? – Stavros_S Dec 07 '16 at 19:21
  • @Stavros_S well it depends on the notation. If it's similar to Western notation except with different characters for decimal point etc, then the process would be almost identical. – Pointy Dec 07 '16 at 19:41
  • I tried adding `'\u066B': '.'` to the number map, and changed the `parseInt()` to a `parseFloat()` but I'm getting an error stating 'input.replace is not a function' – Stavros_S Dec 07 '16 at 19:46
  • @Stavros_S that means that you're not passing a string to the function. – Pointy Dec 07 '16 at 23:56
1

You can try this one , as i am not familiar with this alphabets but it seems to giving the right output . You can modify the code for more efficiency .

function convertToEnglishDigit(input){
 var digit =  ['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹'];
 var store = '';
for(var i = 0; i < input.length; i++){
   for(var j = 0; j < 10; j++){
      if(input[i] === digit[j]) store +=j;
}
}
return parseInt(store);
}

output :

convertToEnglishDigit('۰۱۲۹')
129
convertToEnglishDigit('۰۸۹۲۳۴')
89234
convertToEnglishDigit('۶۱۹۰')
6190
convertToEnglishDigit('۲۱۰')
210
Sifat Haque
  • 5,357
  • 1
  • 16
  • 23