简体   繁体   中英

Java Console (Eclipse, Mac)

Output is 3,141590. Why it is not 3.141590? I am using Eclipse (Java) on a Mac.

public static void main(String[] args) {
    TextIO.putf("%f\n", 3.14159);
}

Thank you

That is because of the Locale. Try this

String.format(Locale.US, "%f\n", 3.14159);

For diferent Locales there are different formats for numbers, dates, encodings, etc.

Comma(,) is coming instead of dot(.). This is because of the locale.

I am giving you one example :

import java.text.NumberFormat;
import java.util.Locale;

public class JavaLocale
{
    public static void main(String[] args) 
    {
        Locale locale = new Locale("da", "DK");
        NumberFormat numberFormat = NumberFormat.getInstance(locale);
        String number = numberFormat.format(100.99);
        System.out.println(number);
    }
}

Output of this code :

100,99

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM