2017-04-07 45 views
-1

我寫了下面的代碼更改號碼波斯工作::不正確的JavaScript

function farsi(x) { 
    x = x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
    var a = '۰۱۲۳۴۵۶۷۸۹'; var b = ''; 
    for (var i = 0; i < x.length; i++) { 
     var c = x.charCodeAt(i); 
     b += (c >= 48 || c <= 57 ? a.charAt(c - 48) : x.charAt(i)); 
    } 
    return b; 
} 

我已經使用正則表達式從How to print a number with commas as thousands separators in JavaScript000 seprator其正常工作。但分隔符不被在這行代碼添加:

b += (c >= 48 || c <= 57 ? a.charAt(c - 48) : x.charAt(i)); 

這裏是fiddle

+2

您在從條件缺少括號:B + =((C> = 48個||ç<= 57)a.charAt(C? - 48):x.charAt(i)); – Esko

+0

https://developer.mozilla.org/cs/docs/Web/JavaScript/Reference/Operators/Operator_Precedence –

+0

@Pavel鏈接導致捷克語翻譯。這裏是英文版:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Operator_Precedence – Fildor

回答