Skip to content

Instantly share code, notes, and snippets.

@alirezas
Created February 13, 2017 10:53
Show Gist options
  • Save alirezas/dcedf8bd2840c2891e50b9becb446c54 to your computer and use it in GitHub Desktop.
Save alirezas/dcedf8bd2840c2891e50b9becb446c54 to your computer and use it in GitHub Desktop.
Convert latin digit to persian in vanilla js
String.prototype.toPersianDigit = function (latinDigit) {
return this.replace(/\d+/g, function (digit) {
var enDigitArr = [], peDigitArr = [], i, j;
for (i = 0; i < digit.length; i += 1) {
enDigitArr.push(digit.charCodeAt(i));
}
for (j = 0; j < enDigitArr.length; j += 1) {
peDigitArr.push(String.fromCharCode(enDigitArr[j] + ((!!latinDigit && latinDigit === true) ? 1584 : 1728)));
}
return peDigitArr.join('');
});
};
function toPersianDigit(digit) {
return digit.toString().toPersianDigit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment