Search code examples
javadoubleexponential

Exponential values vs double ( big decimal conversion already used)


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;
}

Solution

  • 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);
    }