0

I have following line of code:

DecimalFormat df = new DecimalFormat("#.##"); 
System.out.println(df.format(23.00));
System.out.println(Double.parseDouble(df.format(23.00d)));
System.out.println(df.parse("23.00").doubleValue());

First line print 23 but in 2nd and 3rd line when i convert it to double it prints 23.0 . showing 23.0 doesnt makes any sense.

How can i get a double value 23.

I checked these best-way-to-format-a-double-value-to-2-decimal-places how-to-nicely-format-floating-numbers-to-string-without-unnecessary-decimal-0

Community
  • 1
  • 1
Manish Kumar
  • 10,214
  • 25
  • 77
  • 147
  • 2
    `23` and `23.0` are the same thing. If you want to print it without the `.0`, use a format string as in the first line. – tobias_k Jun 30 '14 at 15:02
  • `23` is not a `double`. `23` is an integer. `23.00` is a `double`. If the first line prints the format you want, why parse the output into a double and display the double? It's unclear what you are trying to solve because the answer is within your question. – Brandon Jun 30 '14 at 15:02
  • 1
    In line two and three, you are casting it back to a double (was a String before), so Java will just print them as it always does. – Steffen Jun 30 '14 at 15:02
  • You already have that value. In the last 2 lines you're not formatting the value as you'd like, you're just using `System.out.println(double x)` which by default outputs the value as described by the `Double.toString(double d)` implementation – Morfic Jun 30 '14 at 15:03
  • What exactly do you want anyway? Print it as `23`, which you did in your first line, or print it as `23.00`? – tobias_k Jun 30 '14 at 15:12
  • showing `23.0` doesnt makes any sense.so I want it to be like `23` – Manish Kumar Jun 30 '14 at 15:19

3 Answers3

3

In the second and third output lines, you're just using Double.toString effectively. A double value doesn't remember whatever format it happens to have been parsed in to start with - it's just a numeric value.

If you want to format a value in a particular way, use df.format as you have in your first output line. For example:

String text = ...; // Wherever you get the text from
double value = Double.parseDouble(text);
String formatted = df.format(value); // Using the df you set up before
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Are you saying that every time before showing i should format `23.0` so that it will be displayed as `23` ? can't i store it as `23`? – Manish Kumar Jun 30 '14 at 15:19
  • @Manish: Not unless you want to store it as an int, in which case you couldn't store 23.5 etc. Again: the format isn't part of the value. – Jon Skeet Jun 30 '14 at 15:29
  • yah correct but anyway it will store `23` as `23.0` in db. when i will read it back it will be `23.0` so formatting comes in the picture to make it `23` – Manish Kumar Jun 30 '14 at 15:35
  • @Manish: it doesn't store a textual representation at all. It stores a bunch of bits - the same bits for 23, 23.0, 23.00 etc. How your db stores it is a different matter. – Jon Skeet Jun 30 '14 at 15:37
  • take example of price i am storing it as double so when i store `120` and `150.5` and display back it shows as `120.0` `150.5` not `120` just cos java is treating `120` in double format. – Manish Kumar Jun 30 '14 at 15:43
  • 1
    @Manish: You should use `BigDecimal` instead of double for currency values to start with. But either way, you simply need to understand that when *displaying* a value, you should apply appropriate formatting to it. – Jon Skeet Jun 30 '14 at 16:07
0

A double value, when converted to a String, will NEVER be an integer. It will always contains decimals.

If you want to print to the console a double as 23, you can cast it to an Int:

System.out.println((int) yourDouble);
0

When you say double if your number is 23, it shown 23.0 because it is double.

If your meaning is to print an int value with 2-digits, you can use System.out.printf("%.2d", number);

I don't have any Java compiler on my system this time for test that, but I think it is true.