0

I'm from Sweden, and Eclipse and/or my Java installation seems to know this, because it prints floats with a comma instead of a dot, so 0.01 is written as 0,01, which is the standard here. I want to write some floats to file and then read and plot them with python, so I'm afraid that the commas are going to give me trouble. How can I make Java write to file with the dot instead of comma?

For example:

BufferedWriter bw = null;
try{    
    bw = new BufferedWriter( new FileWriter(filename) );
    bw.write(String.format("%d;%f", 1, 0.000268));
    bw.newLine();
    bw.flush();
} catch(IOException e) {
    e.printStackTrace();
}

should produce:

1;0.000268

instead of

1;0,000268

.

Sahand
  • 7,980
  • 23
  • 69
  • 137
  • 1
    It's locale-specific. If you don't want that, use properly configured `DecimalFormat`. – M. Prokhorov Mar 02 '18 at 12:45
  • 2
    `NumberFormat`: https://stackoverflow.com/a/5054217/6099347 – Aniket Sahrawat Mar 02 '18 at 12:45
  • 1
    https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#format-java.util.Locale-java.lang.String-java.lang.Object...- Pass the locale to use when formatting numbers. – JB Nizet Mar 02 '18 at 12:46
  • 5
    Possible duplicate of [How to change the decimal separator of DecimalFormat from comma to dot/point?](https://stackoverflow.com/questions/5054132/how-to-change-the-decimal-separator-of-decimalformat-from-comma-to-dot-point) – M. Prokhorov Mar 02 '18 at 13:01
  • Make python locale sensitive. You can't? – user unknown Mar 03 '18 at 00:21

1 Answers1

2

As JB Nizet pointed out, you can specify a Locale:

String.format(Locale.US, "%d;%f", 1, 0.000268)
VGR
  • 40,506
  • 4
  • 48
  • 63