Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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; // Convert Arabic numbers
    }).replace(/[۰۱۲۳۴۵۶۷۸۹]/g, function(d) {
        return d.charCodeAt(0) - 1776; // Convert Persian numbers
    }) );
}

Usage:

> parseArabic("۱۶۶۰")
1660

I would suggest you handle it at a lower level: replace the Arabic digits with the corresponding ASCII digits and then convert.

For example:

>a='\u0661\u0666\u0666\u0660'
"١٦٦٠"
>b='\u06f1\u06f6\u06f6\u06f0'
"۱۶۶۰"
>r=/[\u0660-\u0669\u06F0-\u06F9]/g;
/[\u0660-\u0669\u06F0-\u06F9]/g
>a.replace(r,function(c) { return '0123456789'[c.charCodeAt(0)&0xf]; } )
"1660"
>b.replace(r,function(c) { return '0123456789'[c.charCodeAt(0)&0xf]; } )
"1660"