When we work (like multiplication or division) on long and double formats, most of the times we come across exponential form. We offen need it to be converted to decimal form. We can convert exponential form to decimal using class java.math.BigDecimal. We can use BigDecimal.valueOf() to convert the Double value to BigDecimal format and then we can call .toPlainString() to convert it into plain decimal string. Also sometimes as we convert exponential form to decimal , decimal strings can be big, we can trim this using java.text.DecimalFormat as shown in following example.

Double a = 7.77d * 100000000;//Multiply Operation

System.out.println("Large multiply " + a.doubleValue());

System.out.println("Magic of big decimal " + BigDecimal.valueOf(a).toPlainString());

a = 7.77d / 100000;//Devide Operation

System.out.println("Devide operation " + a.doubleValue());

System.out.println("Magic of big decimal " + BigDecimal.valueOf(a).toPlainString());

DecimalFormat formatter = new DecimalFormat("0.000000");

System.out.println("Trimming the big string : "+formatter .format(a));

Output :

Large multiply 7.77E8

Magic of big decimal 777000000

Devide operation 7.769999999999999E-5

Magic of big decimal 0.00007769999999999999

Trimming the big string : 0.000078

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

## 0 comments:

## Post a Comment