Search code examples
javabigdecimal

Big decimal Conversion with roundup value


i have a double value Such as

Double doubleValue=0.0001;

trying to print this gave me an output as 1.0E-4

So I tried BigDecimal to value of this as.

BigDecimal.valueOf(doubleValue) which ended up giving output as "0.00010".

can anyone let me know how would I get a round up value as "0.0001".( no end trail of 0 after 1)


Solution

  • You can try code similar to following

    Double doubleValue=0.0001;    
    DecimalFormat f = new DecimalFormat("##.0000");
    String formattedValue = f.format(doubleValue);
    BigDecimal bigDecimalValue = new BigDecimal(formattedValue);
    bigDecimalValue.stripTrailingZeros();
    

    Hope this helps