-4

I have a Sting value contains in Exponential

class round{    
    public static void main(String args[]){
        String a ="2.4545776339999877E7";
        double roundOff = Math.round(a);
        System.out.println(roundOff);
    }
}

So i want to round off upto 5 decimal vaue. How to do it please?

awksp
  • 11,764
  • 4
  • 37
  • 44

2 Answers2

1

If you want just to output (or get some String) out of this float value you may do this:

double val = Double.valueOf(a);
String str = String.format("%.5f", val);
System.out.println (str);

Else if you really want to get double with 5 signs of precision after integer part, you may write the following:

double val = ... // the same as in the previous example
double val5Signs = Math.floor(val * 1e5) / 1e5;
Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
0

You can also use the Java Decimal-Formatter for Output-Purposes:

double input = Double.valueOf(a);
DecimalFormatSymbols symbol = DecimalFormatSymbols.getInstance();
symbol.setDecimalSeparator('.');
System.out.println(new DecimalFormat("#0.00000", symbol).format(input));
angrybobcat
  • 268
  • 1
  • 10