-1

I written the following code to change the numbers to Persian:

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

I have used the regex for thousand seprator from How to print a number with commas as thousands separators in JavaScript which works correctly. But the separator character is not being added in this line of code:

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

Here is the fiddle

Community
  • 1
  • 1
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171

1 Answers1

5

You put || instead of &&

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

see the working code https://jsfiddle.net/4qvwzs5e/2/

selvarajmas
  • 1,623
  • 13
  • 20