I have a string which has value 7
. I need to convert this value in big decimal equivalent 7.000000
. I tried BigDecimal(String val)
and BigDecimal(BigInteger val, MathContext mc)
constructors of BigDecimal but that did not work they all return 7
. How can I get 7.000000
?
You can use DecimalFormat
to format the output. There is no way to store the unnecessary precision in the BigDecimal
object though.
public static void main(String[] args) {
BigDecimal seven = new BigDecimal(7);
BigDecimal sevenWithDecimals = new BigDecimal("7.12");
DecimalFormat decF = new DecimalFormat("#.000000");
System.out.println(decF.format(seven.doubleValue()));
System.out.println(decF.format(sevenWithDecimals.doubleValue()));
}
output
7.000000
7.120000