Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - replace to English number

Tags:

javascript

I want to replace Bengali numbers to English numbers. such as

 var bengali = [০,১,২,৩,৪,৫,৬,৭,৮,৯];
 var eng = [0,1,2,3,4,5,6,7,8,9];

bengali.replace(eng);

So that anyone write any bengali number it convert to english number. How can I achieve this?

like image 454
Rejoanul Alam Avatar asked Jun 03 '15 19:06

Rejoanul Alam


3 Answers

You have two options, depending on how maintainable you want the code to be.

I would recommend using a hash, keyed by Bengali number, and replacing with the value:

var numbers = {
  '০': 0,
  '১': 1,
  '২': 2,
  '৩': 3,
  '৪': 4,
  '৫': 5,
  '৬': 6,
  '৭': 7,
  '৮': 8,
  '৯': 9
};

function replaceNumbers(input) {
  var output = [];
  for (var i = 0; i < input.length; ++i) {
    if (numbers.hasOwnProperty(input[i])) {
      output.push(numbers[input[i]]);
    } else {
      output.push(input[i]);
    }
  }
  return output.join('');
}

document.getElementById('r').textContent = replaceNumbers('৯  ৭  ৩');
<pre id=r></pre>

If, for some reason, that isn't possible then you can use two arrays and a similar technique to map between them.

like image 98
ssube Avatar answered Oct 20 '22 02:10

ssube


You have everything that you need with just the bengali array (though, you need to wrap each character in quotes) and the built in Array method indexOf(). This code demonstrates it:

var bengali = ["০", "১", "২", "৩", "৪", "৫", "৬", "৭", "৮", "৯"];

console.log(bengali.indexOf("০"));  // index is: 0
console.log(bengali.indexOf("১"));  // index is: 1
console.log(bengali.indexOf("২"));  // index is: 2
console.log(bengali.indexOf("৩"));  // index is: 3
console.log(bengali.indexOf("৪"));  // index is: 4
console.log(bengali.indexOf("৫"));  // index is: 5
console.log(bengali.indexOf("৬"));  // index is: 6
console.log(bengali.indexOf("৭"));  // index is: 7
console.log(bengali.indexOf("৮"));  // index is: 8
console.log(bengali.indexOf("৯"));  // index is: 9

So, all you have to do in your replace function is to return the index of the character that you are checking.

like image 3
talemyn Avatar answered Oct 20 '22 03:10

talemyn


A simple short method to convert Bengali numbers to the equivalent Arabic (English) numbers in Javascript is the following onliner function.

It will also convert Bengali numbers to Arabic in sentences leaving other text intact.

You can call the function on an "onkeyup" event to convert the text immediately.

const toEn = n => n.replace(/[০-৯]/g, d => "০১২৩৪৫৬৭৮৯".indexOf(d));


// Tests
console.log(toEn("৬৭৮৯.৮৯"));                 //6789.89
console.log(toEn("The Number is: ৬৭৮৯.৮৯"));  //The Number is: 6789.89
console.log(toEn("০১২৩৪৫৬৭৮৯.৮৯"));          //0123456789.89
console.log(toEn("০১২৩৪৫৬৭৮৯"));              //0123456789
console.log(toEn("দাম ১২৩৪"));                  //দাম 1234
console.log(toEn("মোট ১২৩৪"));                 //মোট 1234

Updated to add the reverse of changing English (Arabic) numbers to Bengali. This is a shorter one-liner code:

const toBn = n => n.replace(/\d/g, d => "০১২৩৪৫৬৭৮৯"[d])

// Tests
console.log(toBn("6789.89"));                 //৬৭৮৯.৮৯
console.log(toBn("The Number is: 6789.89"));  //The Number is: ৬৭৮৯.৮৯
console.log(toBn("0123456789.89"));           //০১২৩৪৫৬৭৮৯.৮৯
console.log(toBn("0123456789"));              //০১২৩৪৫৬৭৮৯
console.log(toBn("1234"));                    //১২৩৪
like image 3
Mohsen Alyafei Avatar answered Oct 20 '22 03:10

Mohsen Alyafei