java中double与String转换并相加,相减等运算

doubleString进行相互转换,并执行相加、相减等操作,并可以保留一定的小数位数。

1. doubleString 之间的转换

doubleString
  • 使用 Double.toString(double d) 方法:

    double num = 123.456; String str = Double.toString(num);

  • 使用 String.valueOf(double d) 方法:

    double num = 123.456; String str = String.valueOf(num);

Stringdouble
  • 使用 Double.parseDouble(String s) 方法:

    String str = "123.456"; double num = Double.parseDouble(str);

  • 使用 Double.valueOf(String s) 方法:

    String str = "123.456"; double num = Double.valueOf(str);

2. 基本的算术操作

可以直接使用算术运算符进行相加、相减、相乘和相除操作。

public class ArithmeticOperations { 
public static void main(String[] args) { 
double num1 = 10.5; 
double num2 = 3.2; 
// 相加 double sum = num1 + num2; 
System.out.println("Sum: " + sum); 

// 相减 double difference = num1 - num2; 
System.out.println("Difference: " + difference); 

// 相乘 double product = num1 * num2; 
System.out.println("Product: " + product); 

// 相除 double quotient = num1 / num2; 
System.out.println("Quotient: " + quotient); 

// 取余 double remainder = num1 % num2; 
System.out.println("Remainder: " + remainder); } }

3. 保留小数位数

使用 BigDecimal 类可以精确地保留小数位数。

创建 BigDecimal 对象
  • 通过 double 创建:

    BigDecimal bd = new BigDecimal(num);

  • 通过 String 创建:

    BigDecimal bd = new BigDecimal("123.456");

设置小数位数
  • 使用 setScale(int newScale, RoundingMode roundingMode) 方法:
    BigDecimal bd = new BigDecimal("123.456789"); 
    BigDecimal rounded = bd.setScale(2, RoundingMode.HALF_UP); // 保留两位小数,四舍五入 
    System.out.println("Rounded: " + rounded);

4. 综合代码

import java.math.BigDecimal; 
import java.math.RoundingMode; 
public class DoubleStringConversionAndArithmetic { 
public static void main(String[] args) { 
// String 转 double 
String str1 = "45.6789"; 
double num1 = Double.parseDouble(str1); 
System.out.println("String to double: " + num1);

 // double 转 String double num2 = 123.456; 
String str2 = Double.toString(num2); 
System.out.println("Double to String: " + str2); 

// 相加并保留两位小数 double sum = num1 + num2; 
BigDecimal bdSum = new BigDecimal(sum).setScale(2, RoundingMode.HALF_UP);
System.out.println("Sum (2 decimal places): " + bdSum); 

// 相减并保留两位小数 double difference = num1 - num2; 
BigDecimal bdDifference = new BigDecimal(difference).setScale(2, RoundingMode.HALF_UP);
System.out.println("Difference (2 decimal places): " + bdDifference); 

// 相乘并保留两位小数 double product = num1 * num2; 
BigDecimal bdProduct = new BigDecimal(product).setScale(2, RoundingMode.HALF_UP); 
System.out.println("Product (2 decimal places): " + bdProduct); 

// 相除并保留两位小数 double quotient = num1 / num2; 
BigDecimal bdQuotient = new BigDecimal(quotient).setScale(2, RoundingMode.HALF_UP); 
System.out.println("Quotient (2 decimal places): " + bdQuotient); } }

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Fraction类的实现: ```java public class Fraction { private int f1; // 分子 private int f2; // 分母 // 构造方法 public Fraction(int f1, int f2) { this.f1 = f1; this.f2 = f2; } // 分数相加 public Fraction add(Fraction other) { int newF1 = this.f1 * other.f2 + other.f1 * this.f2; int newF2 = this.f2 * other.f2; return new Fraction(newF1, newF2); } // 分数相减 public Fraction subtract(Fraction other) { int newF1 = this.f1 * other.f2 - other.f1 * this.f2; int newF2 = this.f2 * other.f2; return new Fraction(newF1, newF2); } // 分数相乘 public Fraction multiply(Fraction other) { int newF1 = this.f1 * other.f1; int newF2 = this.f2 * other.f2; return new Fraction(newF1, newF2); } // 分数相除 public Fraction divide(Fraction other) { int newF1 = this.f1 * other.f2; int newF2 = this.f2 * other.f1; return new Fraction(newF1, newF2); } // 以a/b的形式打印分数 public String toString() { return f1 + "/" + f2; } // 以浮点数的形式打印分数 public double toDouble() { return (double) f1 / f2; } // 测试Fraction类 public static void main(String[] args) { Fraction f1 = new Fraction(1, 2); Fraction f2 = new Fraction(3, 4); System.out.println(f1.add(f2)); // 5/4 System.out.println(f1.subtract(f2)); // -1/4 System.out.println(f1.multiply(f2)); // 3/8 System.out.println(f1.divide(f2)); // 2/3 System.out.println(f1.toString()); // 1/2 System.out.println(f1.toDouble()); // 0.5 } } ``` 在主方法,我们创建了两个分数对象f1和f2,并对它们进行了加、减、乘、除等运算,并使用toString()方法以a/b的形式打印分数,使用toDouble()方法以浮点数的形式打印分数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值