Skip to content
Amir Iranmanesh edited this page Aug 11, 2026 · 2 revisions

digit

Numbers in Persian text: digit sets, grouping, words and money.

import "github.com/amiranmanesh/go-persian-tools/digit"

Digit sets

Three digit sets show up in Persian input, and they are different code points:

Constant Digits Range
digit.EnglishDigits 0123456789 U+0030–U+0039
digit.PersianDigits ۰۱۲۳۴۵۶۷۸۹ U+06F0–U+06F9
digit.ArabicDigits ٠١٢٣٤٥٦٧٨٩ U+0660–U+0669

Arabic-Indic digits look close enough to Persian ones that they routinely leak into Persian input, so ToEnglishDigits accepts them too.

digit.ToPersianDigits("123salam456")  // ۱۲۳salam۴۵۶
digit.ToPersianDigitsFromInt(1402)    // ۱۴۰۲
digit.ToEnglishDigits("۰۹۱۲٣٤٥٦٧٨٩")  // 09123456789

Filters

Keep only what you want and drop the rest:

digit.OnlyEnglishNumbers("شماره: 0912-345-6789") // 09123456789
digit.OnlyPersianNumbers("a1ب۲")                 // ۲
digit.OnlyNumbers("a1ب۲")                        // 1۲

Decimal points are kept. For Persian letters see text.OnlyPersianAlpha.

Grouping

digit.AddCommas(14555478854)             // "14,555,478,854"
digit.AddCommas(-1234567)                // "-1,234,567"

n, err := digit.RemoveCommas("4,555,478") // 4555478
n, err  = digit.RemoveCommas("۱۲۳،۴۵۶")   // 123456

RemoveCommas returns (int64, error): it accepts ASCII and Persian separators, all three digit sets and surrounding space, and reports an error rather than silently returning zero. digit.ParseInt is the same parser, exposed for reuse.

Words

digit.ToWords(156789) // صد و پنجاه و شش هزار و هفتصد و هشتاد و نه
digit.ToWords(1000)   // هزار
digit.ToWords(-10)    // منفی ده
digit.ToWords(0)      // صفر

digit.ToWord("۱۴۰۲")  // هزار و چهارصد و دو
digit.ToWord("salam") // "" (not a number)

ToWords takes an int64 and handles the full range, including math.MinInt64. ToWord takes a string in any digit set, tolerates separators and a leading minus, and returns the empty string when the input is not an integer.

Scale names go هزار, میلیون, میلیارد, تریلیون, کوادریلیون, کوینتیلیون. Persian says هزار rather than یک هزار for a bare thousand, which the implementation special-cases.

Money

digit.Currency("1234567") // ۱،۲۳۴،۵۶۷
digit.Toman("1234567")    // ۱،۲۳۴،۵۶۷ تومان
digit.Rial("1234567")     // ۱،۲۳۴،۵۶۷ ﷼

Anything that is not a digit is discarded first, so "1234567" and "1,234,567" both work and either digit set is accepted. Grouping uses the Arabic comma ، (digit.ThousandsSeparator), not the ASCII one.

Clone this wiki locally