10.3日报

如下程序:


public class Test {
 public static void main(String[] args) {
  // TODO 自动生成的方法存根
  int intValue=100;
  long longValue=intValue;
  double doubleValue=1234567890;
  float floatValue=(float)doubleValue;
  System.out.println(floatValue);//1.23456794E9
  
  int X=100;
  int Y=200;
  System.out.println("X+Y="+X+Y);
  System.out.println(X+Y+"=X+Y");
  doNotRunme();
  
  String string="";
  double d1=1000.123;
  double d2=1000.123;
  if(Math.abs(d2-d1)<1e-10){
   
  }
  //System.out.println(string);
 }
 public static void doNotRunme()
 {
  doNotRunme();
 }
 }
 程序运行结果是:
1.23456794E9
X+Y=100200
300=X+Y
 
如下程序:
import java.math.BigDecimal;
public class TestBigDecimal {
 public static void main(String[] args) {
  // TODO 自动生成的方法存根
  BigDecimal f1 = new BigDecimal("0.05");
  BigDecimal f2 = BigDecimal.valueOf(0.01);
  BigDecimal f3 = new BigDecimal(0.05);
  System.out.println("下面使用String作为BigDecimal构造器参数的计算结果:");
  System.out.println("0.05 + 0.01 = " + f1.add(f2));
  System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
  System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
  System.out.println("0.05 / 0.01 = " + f1.divide(f2));
  System.out.println("下面使用double作为BigDecimal构造器参数的计算结果:");
  System.out.println("0.05 + 0.01 = " + f3.add(f2));
  System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
  System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
  System.out.println("0.05 / 0.01 = " + f3.divide(f2));
 }
}
 
程序运行结果:
0.05 + 0.01 = 0.06
0.05 - 0.01 = 0.04
0.05 * 0.01 = 0.0005
0.05 / 0.01 = 5
下面使用double作为BigDecimal构造器参数的计算结果:
0.05 + 0.01 = 0.06000000000000000277555756156289135105907917022705078125
0.05 - 0.01 = 0.04000000000000000277555756156289135105907917022705078125
0.05 * 0.01 = 0.0005000000000000000277555756156289135105907917022705078125
0.05 / 0.01 = 5.000000000000000277555756156289135105907917022705078125
 
 
原文地址:https://www.cnblogs.com/cdl-sunshine/p/13765596.html