Shahrooz Jafari Shahrooz Jafari - 1 year ago 84
Javascript Question

JavaScript can't convert Hindi/Arabic numbers to real numeric variables

I'm trying to use a DOM coming from an external source, and in it there are some numeric values in Hindi/Arabic transcription, like

, and when I want to convert it into numeric value I get
What's wrong here?

A little code snippet to be tried:

alert(Number("۱۶۶۰") + ' - ' + Number("1660"));

Answer Source

Well, the Number function does expect the digits 0 to 9 and does not handle arabic ones. You will need to take care of that yourself:

function parseArabic(str) {
    return Number( str.replace(/[٠١٢٣٤٥٦٧٨٩]/g, function(d) {
        return d.charCodeAt(0) - 1632;
    }).replace(/[۰۱۲۳۴۵۶۷۸۹]/g, function(d) {
        return d.charCodeAt(0) - 1776;
    }) );


> parseArabic("۱۶۶۰")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download