Skip to content

toPersianDigits

Homayoun edited this page Jul 9, 2026 · 1 revision

Introduced in v0.2.0

⚠️ Version Recommendation: Always install and use the latest version of rtl-text-tools (npm install rtl-text-tools@latest). While this specific feature was introduced in an earlier version, the latest release includes crucial bug fixes, better edge-case handling, and improved legacy browser compatibility. Avoid pinning your project to an old version just to use this function.

Converts standard Latin digits (0-9) into Extended Persian numerals (Ϋ°-ΫΉ). This is the standard digit format used in Persian (Farsi), Urdu, Dari, and Pashto locales.

(Note: If you need Arabic/Egyptian digits instead, use toArabicDigits(). If you are using the main fixRTL() function, Persian digits are applied automatically by default.)

πŸ“ Syntax

toPersianDigits(text: string): string

Parameters

Name Type Description
text string The text containing Latin digits to convert.

Returns

  • string : The text with all Latin digits replaced by Persian-Indic numerals. Returns the original string unchanged if it is empty, null, or contains no digits.

πŸ’‘ Examples

Basic Usage

import { toPersianDigits } from 'rtl-text-tools';

toPersianDigits("Price 123");     // "Price Ϋ±Ϋ²Ϋ³"
toPersianDigits("Year 2024");     // "Year Ϋ²Ϋ°Ϋ²Ϋ΄"
toPersianDigits("Hello world");   // "Hello world" (No digits to convert)
toPersianDigits("");              // "" (Empty string)

Mixed Content

It safely ignores non-digit characters and only transforms the numbers:

import { toPersianDigits } from 'rtl-text-tools';

toPersianDigits("Total: $50.00"); 
// Output: "Total: $Ϋ΅Ϋ°.Ϋ°Ϋ°"

βš™οΈ Technical Details & Compatibility

  • IE11 Compatibility: The function uses the standard String.prototype.replace() method with a regular expression and a callback function. It intentionally avoids modern methods like String.prototype.replaceAll() to ensure full compatibility with legacy browsers like IE11.
  • Performance: It uses a direct character lookup map (PERSIAN_DIGITS) inside the replace callback, making the conversion extremely fast even for large strings.
  • Safe Execution: If the input is falsy (e.g., null, undefined, or ""), it immediately returns the input without throwing an error.

πŸ“œ Version History

  • v0.2.0: Added toPersianDigits() to convert Latin numbers to Persian numerals (Ϋ°-ΫΉ).

πŸš€ rtl-text-tools

npm downloads

πŸ‘¨β€πŸ’» Created by

Homayoun Mohammadi
πŸ”— GitHub β€’ βœ‰οΈ Email

⭐ Love this project? Star it on GitHub to show your support!

Clone this wiki locally