2

input :123.45.

expect output : 0123.4500

In my netbean 8.02 jdk 8 the result when I run the following code is: 0123,4500

In other compile online site (tutorialspoint/ ideone) the result is : 0123.4500

I don't understand why my result have the comma instead of dot? Anyhelp can give me expect output is 0123.4500 ??

public class MainClass{
     public static void main(String[] args) {
        String out="";
        String Format="0000.0000";
        DecimalFormat dfm=new DecimalFormat(Format);
        out=dfm.format(123.45);
        System.out.println("out="+out);
        }
}
Vu VAnh
  • 70
  • 9
  • Try ####.#### instead of 0000.0000, i think this will help. – amkz Feb 04 '16 at 11:59
  • 1
    it might be a locale issue. please see http://stackoverflow.com/questions/10411414/how-to-format-double-value-for-a-given-locale-and-number-of-decimal-places – bkdev Feb 04 '16 at 12:01
  • 1
    It needs to use localised format (###,###.### 123,456.789 en_US) https://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html – T D Nguyen Feb 04 '16 at 12:02
  • 2
    similar to http://stackoverflow.com/questions/5054132/how-to-change-the-decimal-separator-of-decimalformat-from-comma-to-dot-point – Saril Sudhakaran Feb 04 '16 at 12:11

2 Answers2

3

The comma comes from the Locale on your system. Try setting the default Locale like this

Locale.setDefault(Locale.ENGLISH);

or setting the Locale specificly for this DecimalFormat. Here is one way to do that

NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.ENGLISH);
DecimalFormat dfm = (DecimalFormat) numberFormat;
Simon
  • 6,293
  • 2
  • 28
  • 34
2

I don't understand why my result have the comma instead of dot?

That might be because of your locale setting. There are countries which use comma as a separator for decimals.

Try to set your locale as:

Locale.setDefault(Locale.ENGLISH);

IDEONE DEMO

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331