Skip to content Skip to sidebar Skip to footer

Convert English Numbers To Persian/arabic Only For A Specified Div

I know this question has been replied many times here, but I still haven't got an exact answer.. I need to convert English letters to Persian/Arabic letters by some javascript, but

Solution 1:

I don't believe either of the code samples you provided are JavaScript, the first is close syntactically, but is missing the range() method and new on the array() definition. The second is Java.

To achieve what you require you could convert the text of each of the HTML elements you want to translate to an array and step through them, checking each character via Regex to see if a number was found. If it was, you can do a simple replacement before joining the array back together. Something like this:

var arabicNumbers = ['۰', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
$('.translate').text(function(i, v) {
  var chars = v.split('');
  for (var i = 0; i < chars.length; i++) {
    if (/\d/.test(chars[i])) {
      chars[i] = arabicNumbers[chars[i]];
    }
  }
  return chars.join('');
})
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><divclass="translate">Dummy text with some 123 numbers in 390981 it.</div><divclass="translate">Dummy text with some 67898 numbers in 109209734.09 it.</div>

Update - 2020-03

Here's a shorter version of the above logic using ES6 syntax. Note that this will work in all modern browsers. The only place it won't work is in any version of IE.

var arabicNumbers = ['۰', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
$('.translate').text((i, v) => v.split('').map(c =>parseInt(c) ? arabicNumbers[parseInt(c)] : c).join(''));
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><divclass="translate">Dummy text with some 123 numbers in 390981 it.</div><divclass="translate">Dummy text with some 67898 numbers in 109209734.09 it.</div>

Solution 2:

for use in React , use this Component

importReact, {Component} from"react";


classPersianNumberextendsComponent {


   render() {
       let en_number = this.props.number.toString();
       let persianDigits = "۰۱۲۳۴۵۶۷۸۹";
       let persianMap = persianDigits.split("");
       let persian_number = en_number.replace(/\d/g, function (m) {
         return persianMap[parseInt(m)];
       });

    return (
        <span>{persian_number}</span>
    )


  }

}


exportdefaultPersianNumber

save the file name as : PersianNumber.jsx and use like this :

<PersianNumbernumber={12365}/>

Solution 3:

You can use convertToPersianNumber function that I have copied from this link and use it as the following code in jQuery

$('.translate').text(function(i, v) {
   returnconvertToPersianNumber(v)
})

convertToPersianNumber code

var persianDigits = "۰۱۲۳۴۵۶۷۸۹";
var persianMap = persianDigits.split("");

functionconvertToEnglishNumber(input){
    return input.replace(/[\u06F0-\u06F90]/g, function(m){
        return persianDigits.indexOf(m);
    });
}
functionconvertToPersianNumber(input){
    return input.replace(/\d/g,function(m){
        return persianMap[parseInt(m)];
    });
}

// testsconsole.log(convertToEnglishNumber("۴۳۵"));
console.log(convertToEnglishNumber("۶۲۷۰۱"));
console.log(convertToEnglishNumber("۳۵۴۳"));
console.log(convertToPersianNumber("216541"));
console.log(convertToPersianNumber("16549"));
console.log(convertToPersianNumber("84621"));

Solution 4:

To convert numeric characters, you just need to add/subtract the difference between two sets of Unicode characters to the original numbers. Here is an example:

// English to Persian/Arabicconsole.log(
'Persian now:',
'12345'.replace(/[0-9]/g, c =>String.fromCharCode(c.charCodeAt(0) + 1728))
);

// Persian/Arabic to Englishconsole.log(
'English now:',
'۵۶۷۸۹'.replace(/[۰-۹]/g, c =>String.fromCharCode(c.charCodeAt(0) - 1728))
);

Solution 5:

I think this could help:

const arabicNumbers = ['۰', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
    constThirtyNine = 39;
    constconvertToArabic = (number) => {
      returnString(number).split('').map(char => arabicNumbers[Number(char)]).join('');
    }
    const inArabic = convertToArabic(ThirtyNine);

Post a Comment for "Convert English Numbers To Persian/arabic Only For A Specified Div"