问题
I am still learning Java using Netbeans 11.1.
My problem is when I try to run a program that should display numbers, I get the numbers printed in Arabic which is my Windows default language.
This is what I get as a result:
I added the line: -J-Duser.language=en -J-Duser.region=US
to the netbeans.conf file, but that did not solve the problem.
Another solution suggested using scanner.useLocale(Locale.ENGLISH);
but I could not understand how and where to use it. This is my code:
package lesson02;
public class ProvidedVariablesOneStatement {
public static void main(String[] args) {
String name = "Khalid"; name
int age = 24;
double gpa = 3.40;
System.out.printf("%s is %d years old. %s has %f gpa. \n", name, age, name, gpa);
}
}
The name
is printed in English letters without any problem, but the age
and gpa
are printed in Arabic numbers. The output is:
Khalid is ٢٤ years old. Khalid has ٣٫٤٠٠٠٠٠ gpa.
回答1:
To render the numbers in your output using Western Arabic Numerals you just need to explicitly set the Locale
appropriately within your application.
Here is a slightly modified version of your program which first displays the information using Eastern Arabic numerals, and then displays the same information using Western Arabic numerals.
package javaapplication18;
import java.text.NumberFormat;
import java.util.Locale;
public class JavaApplication18 {
public static void main(String[] args) {
String name = "Khalid";
int age = 24;
double gpa = 3.40;
Locale arLocale = new Locale("ar");
NumberFormat nf = NumberFormat.getInstance(arLocale);
System.out.println("Country: " + arLocale.getCountry() + ", Language: " + arLocale.getLanguage());
System.out.printf("%s is %s years old. %s has %s gpa. \n",
name, nf.format(age), name, nf.format(gpa));
Locale usLocale = new Locale("us", "EN");
nf = NumberFormat.getInstance(usLocale);
System.out.println("Country: " + usLocale.getCountry() + ", Language: " + usLocale.getLanguage());
System.out.printf("%s is %s years old. %s has %s gpa. \n",
name, nf.format(age), name, nf.format(gpa));
}
}
This is what is displayed in the Output window in NetBeans:
Country: , Language: ar
Khalid is ٢٤ years old. Khalid has ٣٫٤ gpa.
Country: EN, Language: us
Khalid is 24 years old. Khalid has 3.4 gpa.
Notes:
- See the Oracle Java Tutorial for an introduction to locales.
- See the Javadocs for Locale and NumberFormat for more details.
- You should also be able to set locale within netbeans.conf, but I chose to do it programmatically to show the effect of switching it dynamically.
- The Output window in NetBeans must be configured to use a font to support the language you are using for output. Obviously this is not an issue for you.
来源:https://stackoverflow.com/questions/58251282/netbeans-11-1-print-numbers-in-windowss-default-language