Converting Arabic numerals to Arabic/Persian numbers in html file
You can use a regular expression to find the parts of the HTML that are between '>' and '<' characters, and operate on those. This will prevent the code from processing the tag names and attributes (style, etc).
// Convert all English digits in a string to Arabic digit equivalents
public static string ToArabicNums(string src)
{
const string digits = "۰۱۲۳۴۵۶۷۸۹";
return string.Join("",
src.Select(c => c >= '0' && c <= '9' ? digits[((int)c - (int)'0')] : c)
);
}
// Convert all English digits in the text segments of an HTML
// document to Arabic digit equivalents
public static string ToArabicNumsHtml(string src)
{
string res = src;
Regex re = new Regex(@">(.*?)<");
// get Regex matches
MatchCollection matches = re.Matches(res);
// process in reverse in case transformation function returns
// a string of a different length
for (int i = matches.Count - 1; i >= 0; --i)
{
Match nxt = matches[i];
if (nxt.Groups.Count == 2 && nxt.Groups[1].Length > 0)
{
Group g = nxt.Groups[1];
res = res.Substring(0, g.Index) + ToArabicNums(g.Value) +
res.Substring(g.Index + g.Length);
}
return res;
}
This isn't perfect, since it doesn't check at all for HTML character specifiers outside of the tags, such as the construct <digits>;
(۱
for ۱, etc)to specify a character by Unicode value, and will replace the digits in these. It also won't process any extra text before the first tag or after the last tag.
Sample:
Calling: ToArabicNumsHtml("<html><body><h1>I was born in 1988</h1></body></html>")
Result: "<html><body><h1>I was born in ۱۹۸۸</h1></body></html>"
Use whatever code you prefer in ToArabicNums
to do the actual transformation, or generalize it by passing in a transformation function.
Convert English numbers to Arabic numerals
If you are referring to what Wikipedia calls eastern arabic / indic numerals, a simple replace operation should do.
$western_arabic = array('0','1','2','3','4','5','6','7','8','9');
$eastern_arabic = array('٠','١','٢','٣','٤','٥','٦','٧','٨','٩');
$str = str_replace($western_arabic, $eastern_arabic, $str);
Convert only Arabic numbers to English in a text input
For some one who does not want to take the pains of finding Number
(as per OP's answer). Here is the updated code :
function parseArabic(){ // PERSIAN (فارسی), ARABIC (عربي) , URDU (اُردُو)
var yas ="٠١٢٣٤٥٦٧٨٩";
yas = (yas.replace(/[٠١٢٣٤٥٦٧٨٩]/g, function (d) {
return d.charCodeAt(0) - 1632;
}).replace(/[۰۱۲۳۴۵۶۷۸۹]/g, function (d) { return d.charCodeAt(0) - 1776; })
);
alert(yas);
}
Convert English numbers to Persian/Arabic only for a specified div
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('');})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class="translate">Dummy text with some 123 numbers in 390981 it.</div><div class="translate">Dummy text with some 67898 numbers in 109209734.09 it.</div>
How to convert Persian and Arabic digits of a string to English using JavaScript?
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
Convert Arabic numbers to English while typing
The following function will change the field key pressed value of the numbers 0123456789 to the Arabic-Eastern form "٠١٢٣٤٥٦٧٨٩".
if you type 1 it will show ١,
if you type 2 it will show ٢, and so on.
It will not affect the other characters.
It can be improved.
document.getElementById('myTextFieldId').addEventListener("keypress", function(e){
let code=e.keyCode-48;
if (code>=0 && code<10) {
e.target.value = e.target.value.slice(0,e.target.selectionStart)
+ "٠١٢٣٤٥٦٧٨٩"[code]
+ e.target.value.slice(e.target.selectionEnd);
e.target.selectionStart = e.target.selectionEnd = e.target.selectionStart + 1;
e.preventDefault();
}
})
<input type="text" id="myTextFieldId" />
Numbers localization in Web applications
A new (to date) and simple JS solution would be to use Intl.NumberFormat. It supports numeral localization, formatting variations as well as local currencies (see documentation for more examples).
To use an example very similar to MDN's own:
const val = 1234567809;
console.log('Eastern Arabic (Arabic-Egyptian)', new Intl.NumberFormat('ar-EG').format(val));
console.log('Persian variant (Farsi)',new Intl.NumberFormat('fa').format(val));
console.log('English (US)',new Intl.NumberFormat('en-US').format(val));
Angular 8 How to read an Arabic characters number as a real integer to convert it into specific format using toLocaleString()
no need to convert chars to Arabic numbers just use:
let enNumber = 89000000;
console.log(enNumber.toLocaleString('fa-IR')); // ۸۹٬۰۰۰٬۰۰۰
Related Topics
Make Overlapping Div "Not Clickable" So That Content Below Can Be Accessed
How to Let a Div Stick to Cursor
Handle Webpack CSS Imports When Testing with Mocha and Babel
Scroll to Bottom in Chat Box in Angularjs
Style Autofill or Autocomplete Default Browser Dropdown
How to Detect If Element Has 'Auto' Height
Css3/Js Get Position of Element During Animation
Implementing Transition Effects in React Js When State Changes
Z-Index Inherits Parents Z-Index , or Not
JavaScript Invalidcharactererror When Modifying a CSS Name with a Space
Gulp with Gulp-Ruby-Sass: Error: ../Style.Css.Map:3:1: Unknown Word
Uncaught Typeerror: Cannot Read Property 'Classname' of Undefined
How to Add CSS Hack Specifically for Ie10