package BigDecimal; import java.math.BigDecimal; import java.lang.Object; public class BigDecimalTest { public static void add(double i, double j){ System.out.println(i+j);//2.5999999999999996 BigDecimal bi = new BigDecimal(1.2); BigDecimal bj = new BigDecimal(1.4); System.out.println(bj);//1.399999999999999911182158029987476766109466552734375 System.out.println(bi);//1.1999999999999999555910790149937383830547332763671875 System.out.println(bi.add(bj));//2.5999999999999998667732370449812151491641998291015625 BigDecimal bis = new BigDecimal("1.2"); BigDecimal bjs = new BigDecimal("1.4"); System.out.println(bjs); //1.2 System.out.println(bis);//1.4 //bis.add(bjs).doubleValue() System.out.println("---:"+bis.add(bjs));//2.6 String si = String.valueOf(i); BigDecimal bsi = new BigDecimal(si); System.out.println(bsi);//1.2 BigDecimal bsii = new BigDecimal(Double.toString(i)); System.out.println(bsii);//1.2 } public static void sub(double i, double j){ System.out.println(j-i); System.out.println(new BigDecimal("1.4").subtract(new BigDecimal("1.2")));// 0.9 } public static void mul(double i, double j){ System.out.println(j*i);//1.68 } public static void div(double i, double j){ System.out.println(i/j);//1.68 } public static void main(String args[]) { double i = 1.2; double j = 1.4; //加法操作 add(i,j); //减法操作 //sub(i, j); //乘法操作 //mul(i, j); //除法操作 div(2.4, 1.2);//2.0 } }
package BigDecimal; import java.math.BigDecimal; public class FloatTest { public static void main(String[] args) { float i = 1.4f; float j = 1.2f; System.out.println(i - j);//0.19999993 BigDecimal bi = new BigDecimal(i); System.out.println(bi);//1.39999997615814208984375 BigDecimal bis = new BigDecimal(String.valueOf(i)); System.out.println(bis); //1.4 BigDecimal bjs = new BigDecimal(String.valueOf(j)); System.out.println(bjs); //1.2 System.out.println(bis.subtract(bjs).floatValue()); //0.2 System.out.println(bis.subtract(bjs).doubleValue());//0.2 } }