0

I want to get a function which rounds the double value into two significant digits now i m using the below function

private static DecimalFormatSymbols DFS;
private static DecimalFormat myFormatter;
public static String DoubleToFormatedString(double value) {
    DFS = new DecimalFormatSymbols();
    DFS.setDecimalSeparator('.');
    myFormatter = new DecimalFormat("############.##");
    myFormatter.setDecimalFormatSymbols(DFS);
    return myFormatter.format(value);
}

but with this function eg: i m getting a rounded number 2.6, i need to get this like 2.60. '0'should come to the right of 1 significant num. what change should make? or any other way plz help me

alexgophermix
  • 4,189
  • 5
  • 32
  • 59
Jesbin MJ
  • 3,219
  • 7
  • 23
  • 28

3 Answers3

2

Replace your formatter as below

myFormatter = new DecimalFormat("#.00");

For further details you can refer to this SO question : Format double to 2 decimal places with leading 0s

Community
  • 1
  • 1
prashant
  • 1,805
  • 12
  • 19
0

try

new DecimalFormat("#.00");
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

Yes just use

DecimalFormat myFormatter = new DecimalFormat("#.00");

instead of

 DecimalFormat myFormatter = new DecimalFormat("############.##");
Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65