-3

i am using following function to format the double value to x.xx , but after that the result value started to end up with exponential value ( i have already read the answers which are advising to use the DecimalFormat or to plain text () methods but every where the system is giving error to change the types ...return types i am lost a bit now

can some one please help me in following method tweaks so that the returning values can be displayed as normal number instead of 3.343E32

Please note that the following function FD is used many times in code so i want to change it here only so i dont have to apply the formatting each and every results / variable..

public double fd(double x) {
    BigDecimal bd_tax = new BigDecimal(x);
    BigDecimal formated = bd_tax.setScale(2, BigDecimal.ROUND_UP);
    x = Double.valueOf(formated.doubleValue());
    return x;
}
Wooble
  • 87,717
  • 12
  • 108
  • 131
Nayyar
  • 17
  • 4

1 Answers1

1

You say, "...to format the double value...", but there is no code in your example that formats anything. Your fd(x) function takes a double as its argument, and it returns a double. It doesn't do anything else.

If your numbers really are in the neighborhood of 3.343E32, Then they're going to have a lot of digits: 33 digits before the decimal point. Is that what you were expecting?

Suggest you look at String.format() if you are trying to turn the double value into human readable text. Something like this, perhaps:

public String fd(double x) {
    return String.format("%.2f", x);
}
Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • +1 You can format a String, but a number is just a value it doesn't have a format. – Peter Lawrey Aug 13 '14 at 22:06
  • i am new to java let me rephrase the problem in my code one variable double was getting displayed as 23.2222222222 so i use the above function which takes & return double after formatting the variable in form 23.33 by using the big decimal which solves the issue of 23.22222 to 23.22 but 2nd problem is that after big multiplication the result ( double variable ) started to displayed as exponential values ... and i need to get rid of the exponential value for the variable whcih is getting return in function fd.... FYI i have already tried the DecimalFormat function and .toplaintext() function – Nayyar Aug 15 '14 at 07:19