在html页面中将英文数字转换为阿拉伯数字

Sar*_*rah 18 javascript css ascii arabic

我需要将出现在给定HTML页面中的所有英文数字转换为阿拉伯语数字(独立于用户浏览器编码).我更喜欢使用javascript,如果可以使用CSS处理它会很棒.

我找到了一些页面,但我发现阿拉伯字母在源代码中添加了ASCII表示.这是否意味着他们正在应用某种java脚本函数?

任何线索我怎么能做这样的事情?

ken*_*bec 38

直接更换功能怎么样?

String.prototype.toIndiaDigits= function(){
 var id= ['?','?','?','?','?','?','?','?','?','?'];
 return this.replace(/[0-9]/g, function(w){
  return id[+w]
 });
}

// test

var S='The year 2009 has only 365 days';
alert(S.toIndiaDigits());

/*  returned value: (String)
The year ???? has only ??? days
*/
Run Code Online (Sandbox Code Playgroud)

  • 您可以将 `id=[...` 替换为 `id='0123456789'` (2认同)

小智 11

您将需要使用JavaScript,但该过程非常简单.假设您要转换的数字已经在字符串中,那么类似以下代码片段的内容将起作用:

function convertDigitIn(enDigit){ // PERSIAN, ARABIC, URDO
    var newValue="";
    for (var i=0;i<enDigit.length;i++)
    {
        var ch=enDigit.charCodeAt(i);
        if (ch>=48 && ch<=57)
        {
            // european digit range
            var newChar=ch+1584;
            newValue=newValue+String.fromCharCode(newChar);
        }
        else
            newValue=newValue+String.fromCharCode(ch);
    }
    return newValue;
}
Run Code Online (Sandbox Code Playgroud)

代码不是很漂亮,并且可能更有效地编写,但实质上它正在做的是通过添加偏移值将任何字符串从"0"转换为"9",以使字符值现在处于unicode范围内印度数字.印度数字的范围从\ u0660到\ u0669,因此从欧洲到印度数字的转换只是简单的数学.


McD*_*ell 7

解释这个评论:

就像在这个链接almasry-alyoum.com当我查看这个页面的来源时,我发现印度字母被放在他们的ascii表示中(即&#1634;&#1635;&#1639;)

这些是HTML字符实体.值是文档定义的Unicode代码点.

0660 ARABIC-INDIC DIGIT ZERO
0661 ARABIC-INDIC DIGIT ONE
0662 ARABIC-INDIC DIGIT TWO
0663 ARABIC-INDIC DIGIT THREE
0664 ARABIC-INDIC DIGIT FOUR
0665 ARABIC-INDIC DIGIT FIVE
0666 ARABIC-INDIC DIGIT SIX
0667 ARABIC-INDIC DIGIT SEVEN
0668 ARABIC-INDIC DIGIT EIGHT
0669 ARABIC-INDIC DIGIT NINE
Run Code Online (Sandbox Code Playgroud)

因此,0 1 2 3 4 5 6 7 8 9可以编码为&#x0660; &#x0661; &#x0662; &#x0663; &#x0664; &#x0665; &#x0666; &#x0667; &#x0668; &#x0669;网页.

注意:&#对于十进制值; &#x对于十六进制


小智 6

我知道这是一篇很老的帖子,但对于从 google 搜索来到这里的其他人来说,有同样的问题,有一个名为toLocaleString的相对较新的方法,它可以将 Number 类型转换为您喜欢的数字系统字形:

(2500000).toLocaleString('ar-EG');
//outputs: "?????????"
Run Code Online (Sandbox Code Playgroud)


Nic*_*ell 5

谢谢你的回答.没有人讨论过处理小数和千位标记.例如,请参阅维基百科.根据此页面,这些是正确的unicode字符:

  • U + 066B - 阿拉伯语十进制分隔符
  • U + 066C - 阿拉伯数千名分隔符


P9Q*_*P9Q 5

将英语(拉丁)数字转换为波斯数字和阿拉伯数字。

//English to Persian digits.
String.prototype.toFa= function() {
  return this.replace(/\d/g, d => '??????????'[d])
}

//English to Arabic digits.
String.prototype.toAr= function() {
  return this.replace(/\d/g, d =>  '??????????'[d])
}

//English to either Persian or Arabic digits.
String.prototype.toIn= function(e) {
  return this.replace(/\d/g, d => e ? '??????????'[d] : '??????????'[d])
}

//English to Persian digits using unicode.
String.prototype.toFaUni= function() {
  return this.replace(/\d/g, d => String.fromCharCode('0x06F'+d))
}

//English to Arabic digits using unicode.
String.prototype.toArUni= function() {
  return this.replace(/\d/g, d => String.fromCharCode('0x066'+d))
}

//English to either Persian or Arabic digits.
String.prototype.toInUni= function(e) {
  return this.replace(/\d/g, d => String.fromCharCode('0x06'+(e ? '6':'F')+d))
}

//examples
let text = 'It is 30/08/2018 at 8:24 AM'

//using array
alert(text.toFa())
alert(text.toAr())
alert(text.toIn(0))
alert(text.toIn(1))

//using unicode
alert(text.toFaUni())
alert(text.toArUni())
alert(text.toInUni(0))
alert(text.toInUni(1))
Run Code Online (Sandbox Code Playgroud)

jsfiddle