Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

displaying numbers in fa_IR locale

I'm trying to display numbers in fa_IR locale.
I'm using this code:

NumberFormat.getNumberInstance(new Locale("fa","IR")).format(myNum);

But this prints English (en_US) numbers.
How can I display a number in a specific locale in Java?


Example:
For example if myNum be 12 the output should be ۱۲


Edit:
It seems java.text.NumberFormat doesn't support fa_IR locale (?).
Using com.​ibm.​icu.​text.​NumberFormat instead, fixes the problem!

like image 557
Ariyan Avatar asked Dec 16 '22 18:12

Ariyan


2 Answers

As you know Oracle (or it's better to say Java Programming language ) does not support Persian directly, in order to have a Persian( or Arabic ) NumberFormat you can use the below codes:

NumberFormat numberFormat = NumberFormat.getNumberInstance(new Locale.Builder().setLanguageTag("ar-SA-u-nu-arab").build());
System.out.println(numberFormat.format(1234567890));

The output will be like this: ۱،۲۳۴،۵۶۷،۸۹۰ as you can see by using this kind of code your output number not only will be in Persian style but also its each three numbers will be grouped and separated by a "," character.

like image 104
Elyas Hadizadeh Avatar answered Dec 27 '22 23:12

Elyas Hadizadeh


Try this code:

NumberFormat nf = NumberFormat.getNumberInstance(new Locale("fa"));
System.out.println(nf.format(1234567890));

output:

۱۲۳٬۴۵۶٬۷۸۹
like image 38
Alireza Noorali Avatar answered Dec 28 '22 00:12

Alireza Noorali