Persian numbers in Ruby

- 1 min

As everyone knows, Computer is created based on English language. Other Languages are involved later. There are some issues when working on an application which is used in Non English languages like Persian language which is my Native language.

One of the small issues is about numbers. When a system user enters numbers by keyboard, saving characters will be different based on keyboard layout configuration. Therefore, texts displayed on your application will in good format if all of numbers are displayed in English or Persian format.

I have had this problem all the times. The simple way to solve this is just converting Array of numbers in two language format together. Take below Ruby code as an example.

# get date in Persian format
"1395-06-26".tr('0123456789', '۰۱۲۳۴۵۶۷۸۹')
# => "۱۳۹۵-۰۶-۲۶"

# get email in English number format
"test۵@example.com".tr('۰۱۲۳۴۵۶۷۸۹', '0123456789')
# => "test5@example.com"

This issue appears on every project. Therefore, I made a simple gem called nums2fa to do it more easily.

Previous code is rewritten below With nums2fa

require 'nums2fa'

# get date in Persian format
"1395-06-26".nums2fa
# => "۱۳۹۵-۰۶-۲۶"

# get email in English number format
"test۵@example.com".nums2en
# => "test5@example.com"
Mohsen Alizadeh

Mohsen Alizadeh

I am a Software Developer living in Frankf.
My interests span Backend Development with Ruby On Rails && Elixir, System Architecture Design, Sytem Reliability, and System Scalability.

rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora