Number format in java

advertisements

Is there a way to do a format on double to String in java where i would want the output as:

Intended output:

if n=12.4 => 12.4
if n=12.0 -> 12

I was using String.valueof(n) with returns Current output:
if n=12.4 => 12.4
if n=12.0 => 12.0

Any help will be appreciated.


You can use DecimalFormat:

System.out.println(new DecimalFormat("0.#").format(12.0));