简体   繁体   中英

Eclipse double separator from comma(,) to dot(.)

On my Eclipse the double separator is using comma instead of dot and I would like to change it to comma but I am not sure how. The code itself is as follow:

double latitudeBerlin = 52.51217;
double longitudeBerlin = 13.42106;
String policyBerlin = String.format("a:%f:%f:22:1", latitudeBerlin, longitudeBerlin);
System.out.println(policyBerlin);

It returns a:52,512170:13,421060:22:1 but in other computer's Eclipse it gave a:52.512170:13.421060:22:1 and I would like to make it like so. How can i do it?

It is using the computers default Locale's decimal seperator. This is why it is system depended.

You can use something like:

String.format(Locale.ROOT, "%.2f", someDouble);

This will ensure that you always get the same decimal seperator. Locale.ROOT is regarded as the base locale for all locales. Root locale language, country, and variants are empty, and uses dot as default decimal seperator.

Example:

String.format(Locale.ROOT, "a:%f:%f:22:1", latitudeBerlin, longitudeBerlin);

Output: a:52.512170:13.421060:22:1

String.format(Locale.GERMAN, "a:%f:%f:22:1", latitudeBerlin, longitudeBerlin);

Output: a:52,512170:13,421060:22:1

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