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
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
See more on this question at Stackoverflow