Performing digital operation using BigDecimal

Digital calculation, calculation may be performed using the BigDecimal.

String number = "100";
BigDecimal decimal1 = new BigDecimal(number);
String number1 = "10";
BigDecimal decimal2 = new BigDecimal(number1);
System.out.println("加法:"+number+"+"+number1+"="+decimal1.add(decimal2).toString());
System.out.println("减法:"+number+"-"+number1+"="+decimal1.subtract(decimal2).toString());
System.out.println("乘法:"+number+"*"+number1+"="+decimal1.multiply(decimal2).toString());
System.out.println("除法:"+number+"/"+number1+"="+decimal1.divide(decimal2).toString());

These are the four basic operations, as well as a mean, I did not write it.

It is important to note that the use

BigDecimal decimal1 = new BigDecimal(number);

When you create a BigDecimal objects, number can not be empty, if empty, then there will be an exception.

This time on the first record to be here, if there are problems later records.

Guess you like

Origin www.cnblogs.com/lizs0314/p/11325184.html