Shahrooz Jafari Shahrooz Jafari - 3 months ago 12
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
NaN
.
What's wrong here?

A little code snippet to be tried:

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

Answer

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;
    }) );
}

Usage:

> parseArabic("۱۶۶۰")
1660