I'm building a software that takes integers from users and does some calculations and then outputs the result. The thing is that I want to take users numbers using English numbers(0, 1, 2, etc.)
and I want to present the numbers using Persian numbers(like Arabic) in the output. I've read some topics on Unicode conversion and things like replace()
and charCodeAt()
but I can't understand the code.
Here's a piece of code.(It converts Persian numbers into English numbers but I want to do the opposite.)
var yas ="٠١٢٣٤٥٦٧٨٩";
yas = Number(yas.replace(/[٠١٢٣٤٥٦٧٨٩]/g, function (d) {
return d.charCodeAt(0) - 1632;
}).replace(/[۰۱۲۳۴۵۶۷۸۹]/g, function (d) { return d.charCodeAt(0) - 1776; })
);
Unicode in JavaScript. ES2015 specification mentions that source code text is expressed using Unicode (version 5.1 and above). The source text is a sequence of code points from U+0000 to U+10FFFF .
In Javascript, the identifiers and string literals can be expressed in Unicode via a Unicode escape sequence. The general syntax is \uXXXX , where X denotes four hexadecimal digits. For example, the letter o is denoted as '\u006F' in Unicode.
To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X.
That Persian-to-English script seems unnecessarily complicated, which makes me wonder if I'm missing something.
Basically, with such a limited data set, the simplest thing is to give yourself a map either way:
// The "Persian" here aren't just Persian, nor are the English just English.
// Both numeral sets are used in multiple languages...
// One time setup
var persian ="٠١٢٣٤٥٦٧٨٩";
var mapPtoE = Object.create(null);
var mapEtoP = Object.create(null);
persian.split("").forEach(function(glyph, index) {
mapPtoE[glyph] = index;
mapEtoP[index] = glyph;
});
// Convert one char "Persion" => "English"
function charPtoE(ch) {
return mapPtoE[ch] || ch;
}
// Convert one char "English" => "Persion"
function charEtoP(ch) {
return mapEtoP[ch] || ch;
}
// Convert the "Persian" digits in a string to "English"
function strPToE(s) {
return s.replace(/[٠١٢٣٤٥٦٧٨٩]/g, charPtoE);
}
// Convert the "English" digits in a string to "Persian"
function strEToP(s) {
return s.replace(/\d/g, charEtoP);
}
// Demonstrate converting "Persian" to "English"
console.log("Test A ٠١٢٣", "=>", strPToE("Test A ٠١٢٣"));
console.log("Test B ٦٥٤", "=>", strPToE("Test B ٦٥٤"));
console.log("Test C ٧٨٩", "=>", strPToE("Test C ٧٨٩"));
// Demonstrate converting "English" to "Persian"
console.log("Test A 0123", "=>", strEToP("Test A 0123"));
console.log("Test B 654", "=>", strEToP("Test B 654"));
console.log("Test C 789", "=>", strEToP("Test C 789"));
From your question it looks like there can be more than one form for 4 and 6 (pardon my ignorance); if so, you'll want to adjust the above to handle that in the "Persian" to "English" conversion, and pick one to use going the other way.
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