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?
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.
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.
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")); //১২৩৪
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With