让我们用字符串创建BigDecimal值
BigDecimal val1 = new BigDecimal("37578975.8768"); BigDecimal val2 = new BigDecimal("62567878.9768");
您也可以将以上设置为-
String str1 = “37578975.8768”; BigDecimal val1 = new BigDecimal(str1); String str2 = “62567878.9768”; BigDecimal val2 = new BigDecimal(str2);
现在,您还可以执行如下操作:
val1 = val1.add(val2); System.out.println("Addition Operation = " + val1);
以下是一个例子-
import java.math.BigDecimal; public class Demo { public static void main(String[] argv) throws Exception { //通过字符串创建BigDecimal值 BigDecimal val1 = new BigDecimal("37578975.8768"); BigDecimal val2 = new BigDecimal("62567878.9768"); System.out.println("Value 1 : "+val1); System.out.println("Value 2 : "+val2); val1 = val1.add(val2); System.out.println("Addition Operation = " + val1); val1 = val1.multiply(val2); System.out.println("Multiplication Operation = " + val1); } }
输出结果
Value 1 : 37578975.8768 Value 2 : 62567878.9768 Addition Operation = 100146854.8536 Multiplication Operation = 6265976294387200.48179648