Created
February 13, 2017 10:53
-
-
Save alirezas/dcedf8bd2840c2891e50b9becb446c54 to your computer and use it in GitHub Desktop.
Convert latin digit to persian in vanilla js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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