两个float或double的数相减会出现精度丢失的问题
package com.hz;import java.math.BigDecimal;public class Test { public static void main(String[] args) { float a = 50.01f; float b = 50.00f; float ss = a - b; BigDecimal b1 = new BigDecimal(Float.toString(a)); BigDecimal b2 = new BigDecimal(Float.toString(b)); float tt = b1.subtract(b2).floatValue(); System.out.PRintln(ss); System.out.println(tt); }}结果为:0.00999832150.01
新闻热点
疑难解答