Skip to content

Instantly share code, notes, and snippets.

@1mehdifaraji
Created August 29, 2025 11:01
Show Gist options
  • Select an option

  • Save 1mehdifaraji/7ccb4f6fbf1acb2c333fee487f33c964 to your computer and use it in GitHub Desktop.

Select an option

Save 1mehdifaraji/7ccb4f6fbf1acb2c333fee487f33c964 to your computer and use it in GitHub Desktop.
Converting English (Latin) numbers to Persian and vice versa.
export const e2p = (input: string | number) => {
const persianDigits = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
return input.toString().replace(/\d/g, (d) => persianDigits[parseInt(d)]);
};
export const p2e = (input: string | number) => {
const persianDigits = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
return input
.toString()
.replace(/[۰-۹]/g, (d) => persianDigits.indexOf(d).toString());
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment