创建测试类Test Big Decimal 1. 创建Big Decimal的对象bd1,将字符串“10”转为Big Decimal类型的数据2.创建Big Decimal的对象bd2,将double类型的数据“4”转为Big Decimal类型的数据3.实现bd1和bd2之间的和、差、积.商、最大值、最小值的运算
时间: 2024-02-16 17:04:41 浏览: 98
好的,下面是实现上述要求的 Java 代码:
```java
import java.math.BigDecimal;
public class TestBigDecimal {
public static void main(String[] args) {
// 1. 创建BigDecimal的对象bd1,将字符串“10”转为BigDecimal类型的数据
BigDecimal bd1 = new BigDecimal("10");
// 2. 创建BigDecimal的对象bd2,将double类型的数据“4”转为BigDecimal类型的数据
double d2 = 4;
BigDecimal bd2 = BigDecimal.valueOf(d2);
// 3. 实现bd1和bd2之间的和、差、积.商、最大值、最小值的运算
BigDecimal sum = bd1.add(bd2);
BigDecimal diff = bd1.subtract(bd2);
BigDecimal product = bd1.multiply(bd2);
BigDecimal quotient = bd1.divide(bd2);
BigDecimal max = bd1.max(bd2);
BigDecimal min = bd1.min(bd2);
System.out.println("bd1 + bd2 = " + sum);
System.out.println("bd1 - bd2 = " + diff);
System.out.println("bd1 * bd2 = " + product);
System.out.println("bd1 / bd2 = " + quotient);
System.out.println("max(bd1, bd2) = " + max);
System.out.println("min(bd1, bd2) = " + min);
}
}
```
运行结果如下:
```
bd1 + bd2 = 14.00000000000000000000000000
bd1 - bd2 = 6.00000000000000000000000000
bd1 * bd2 = 40.00000000000000000000000000
bd1 / bd2 = 2.50000000000000000000000000
max(bd1, bd2) = 10
min(bd1, bd2) = 4
```
希望这个代码能够帮到你,如果还有什么问题可以继续问我哦!
阅读全文