Skip to content

Instantly share code, notes, and snippets.

@miladd3
Last active December 17, 2024 10:55
Show Gist options
  • Save miladd3/f7b41598897c2147db16d9770ae1f281 to your computer and use it in GitHub Desktop.
Save miladd3/f7b41598897c2147db16d9770ae1f281 to your computer and use it in GitHub Desktop.
latin to Persian number pure JS function
/**
* Converts a Latin number to its Persian numeral representation.
*
* @param {string|number} v - The input number or string to convert.
* @returns {string} - The Persian numeral representation of the input.
*/
function toPersianNumber(v) {
const PersianNumber = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
const vString = v.toString();
const chars = vString.split('').map(char => /\d/.test(char) ? PersianNumber[char] : char);
return chars.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment