Shahrooz Jafari Shahrooz Jafari - 5 months ago 17
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"));


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("۱۶۶۰")