1、float和double只能用来作科学计算或者是工程计算,但在商业计算中我们要用java.math.BigDecimal
2、注意:double 和float转换为字符串类型造成进度丢失;直接用字符串表示double 和float
3、案例:
package com.zhangwl.pg1113; import org.junit.Test; import java.math.BigDecimal; /** * @ClassName RunMain * @Description 演示小案例 * @Author zhangwl * @Date 2019/11/13 23:02 * @Version 1.0 **/public class RunMain { @Test public void test_01() { BigDecimal douDecMax = new BigDecimal(2.0); BigDecimal douDecMin = new BigDecimal(1.10); System.out.println("douDecMax - douDecMin = " + douDecMax.subtract(douDecMin)); System.out.println("********************分界线*************************"); BigDecimal strDecMax = new BigDecimal("2.0"); BigDecimal strDecMin = new BigDecimal("1.10"); System.out.println("strDecMax - strDecMin = " + strDecMax.subtract(strDecMin)); System.out.println(); }} 运行结果:
douDecMax - douDecMin = 0.899999999999999911182158029987476766109466552734375
********************分界线*************************
strDecMax - strDecMin = 0.90
原文地址:https://www.cnblogs.com/sico/p/11854056.html
时间: 2024-10-02 03:50:28