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,014
  • 5
  • 31
  • 55
Jesbin MJ
  • 3,049
  • 6
  • 22
  • 27

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,785
  • 11
  • 19
0

try

new DecimalFormat("#.00");
Evgeniy Dorofeev
  • 129,181
  • 28
  • 195
  • 266
0

Yes just use

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

instead of

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