I read this article: JDK 8 and JRE 8 Supported Locales, it stated that:
Numbering systems can be specified by a language tag with a numbering system ID
╔═════════════════════╦══════════════════════╦══════════════════╗ ║ Numbering System ID ║ Numbering System ║ Digit Zero Value ║ ╠═════════════════════╬══════════════════════╬══════════════════╣ ║ arab ║ Arabic-Indic Digits ║ \u0660 ║ ╚═════════════════════╩══════════════════════╩══════════════════╝
Now, to demonstrate this, I wrote the following codes:
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;
public class Main
{
public static void main(String[] args)
{
Locale locale = new Locale("ar", "sa", "arab");
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale);
NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
System.out.println(dfs.getZeroDigit());
System.out.println(numberFormat.format(123));
}
}
I was expecting the output to be something like:
٠
١٢٣
However, the output is as follows:
0
123
The main purpose of this is to make JavaFX GUI showing Arabic numbers instead of English numbers as it uses the default locale (and I can set it with Locale.setDefault(...)
).
So my question is, how to use the numbering system in the locale to show localized numbers in Java? Then, is it possible to apply it on JavaFX?
While the Locale
in the accepted answer works well with NumberFormat
; DateTimeFormatter
(and prolly all other java.time
formatters) fail to use it properly, at least before the introduction of localizedBy()
in JDK 10.
So for folks who can't use JDK 10+ and are looking to get a fully localized string representation of the date, one alternative is to do (Kotlin):
// code in the oh-so-sweet-and-sugary Kotlin
val locale = Locale.Builder()
.setLanguageTag("ar-SA-u-nu-arab")
.build()
val numFormat = NumberFormat.getNumberInstance(locale)
val formatter = DateTimeFormatter.ofPattern("yyyy MM dd", locale)
val str = formatter.format(OffsetDateTime.now())
// String.replace(Regex, noninline (MatchResult) -> CharSequence)
// this method is gold
str.replace(Regex("\\d"), {numFormat.format(it.value.toInt())}))
println(str)
I feel like this could be done in a better/more elegant way. If anyone has it, please do share.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With