java double 加减乘除
时间: 2024-12-28 20:03:15 浏览: 9
在Java中,`double`类型用于表示双精度浮点数。以下是一些关于`double`类型的基本操作,包括加、减、乘、除:
### 加法
```java
double a = 10.5;
double b = 5.2;
double sum = a + b; // 结果为 15.7
System.out.println("Sum: " + sum);
```
### 减法
```java
double a = 10.5;
double b = 5.2;
double difference = a - b; // 结果为 5.3
System.out.println("Difference: " + difference);
```
### 乘法
```java
double a = 10.5;
double b = 5.2;
double product = a * b; // 结果为 54.6
System.out.println("Product: " + product);
```
### 除法
```java
double a = 10.5;
double b = 5.2;
double quotient = a / b; // 结果为 2.019230769230769
System.out.println("Quotient: " + quotient);
```
### 示例代码
```java
public class DoubleOperations {
public static void main(String[] args) {
double a = 10.5;
double b = 5.2;
double sum = a + b;
double difference = a - b;
double product = a * b;
double quotient = a / b;
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
}
}
```
运行上述代码将输出:
```
Sum: 15.7
Difference: 5.3
Product: 54.6
Quotient: 2.019230769230769
```
阅读全文