Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display Persian(Farsi) numbers in views

I want to display Persian(Farsi) numbers on views. For example I calculated a date and converted it to Jalali calendar but how can I display it by Persian numbers?

like image 943
Farnad Tohidkhah Avatar asked Jul 22 '15 09:07

Farnad Tohidkhah


People also ask

How are Persian numbers written?

Persian numbering rules Digits from zero to nine are specific words, namely sefr (صفر) [0], yek (یک) [1], do (دو) [2], se (سه) [3], chahâr (چهار) [4], panj (پنج) [5], shesh (شش) [6], haft (هفت) [7], hasht (هشت) [8], and noh (نه) [9].


1 Answers

Another way to show numbers with Persian font is the use of following Helper Class:

public class FormatHelper {      private static String[] persianNumbers = new String[]{ "۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹" };       public static String toPersianNumber(String text) {         if (text.length() == 0) {             return "";         }         String out = "";         int length = text.length();         for (int i = 0; i < length; i++) {         char c = text.charAt(i);         if ('0' <= c && c <= '9') {             int number = Integer.parseInt(String.valueOf(c));             out += persianNumbers[number];         }         else if (c == '٫') {             out += '،';         }         else {             out += c;         }          return out;     } } 

Save this class as UTF8 format and use it like the following code

FormatHelper.toPersianNumber(numberString); 
like image 86
Farnad Tohidkhah Avatar answered Sep 19 '22 22:09

Farnad Tohidkhah