2

I want change latin number to persian number and add commas, but it don't work true for me. what do i do?

Demo:
Please see my demo, output is &#1,782;&#1,783;&#1,783;&#1,782;&#1,781;&#1,781;&#1,782;&#1,783;&#1,783;&#1,784;&#1,784;&#1,785;&#1,785;&#1,784;&#1,783;&#1,784;&#1,780;&#1,776;&#1,784;&#1,776; not a right number.

function numentofa(n) {
var digits = [],
    r;
do {
    r = n % 10;
    n = (n - r) / 10;
    digits.unshift(['&#', r + 1776, ';'].join(''));
} while (n > 0);
return digits.join('');
}
function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}
var oooook = addCommas(numentofa(67765567788998776676));
//$('.numbers').append(oooook);
    alert(oooook);​
3
  • @mplungjan No, it is not me purpose in life to debug code which is not mine. The post should contain more relevant information, including "what" and "which part" doesn't work. Is it the conversion from Persian? If so, please remove all the irrelevant stuff about commas.
    – user166390
    Aug 31, 2012 at 19:17
  • Please see my demo, output is &#1,782;&#1,783;&#1,783;&#1,782;&#1,781;&#1,781;&#1,782;&#1,783;&#1,783;&#1,784;&#1,784;&#1,785;&#1,785;&#1,784;&#1,783;&#1,784;&#1,780;&#1,776;&#1,784;&#1,776; not a right number. Aug 31, 2012 at 19:18
  • 1
    @jenniferJolie &#XXXX; means nothing in JavaScript. It is an HTML escape. Use '\uXXXX' in a JavaScript literal but to build from characters dynamically, use String.fromCharCode(xxxx)
    – user166390
    Aug 31, 2012 at 19:20

1 Answer 1

4

Instead of using HTML character codes, use actual characters:

digits.unshift(String.fromCharCode(r+1776));

And instead of regexing on digits generalise it to any non-comma character:

var rgx = /([^,]+)([^,]{3})/;

Updated Fiddle

1
  • In the output between numbers is ، not comma ,. ? Aug 31, 2012 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.