2019独角兽企业重金招聘Python工程师标准>>>
infinity:无穷的; finity:有限的; 看了下jdk1.8中Double的API,有两个方法:
/*** Returns {@code true} if the specified number is infinitely* large in magnitude, {@code false} otherwise.** @param v the value to be tested.* @return {@code true} if the value of the argument is positive* infinity or negative infinity; {@code false} otherwise.*/public static boolean isInfinite(double v) {return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);}/*** Returns {@code true} if the argument is a finite floating-point* value; returns {@code false} otherwise (for NaN and infinity* arguments).** @param d the {@code double} value to be tested* @return {@code true} if the argument is a finite* floating-point value, {@code false} otherwise.* @since 1.8*/public static boolean isFinite(double d) {return Math.abs(d) <= DoubleConsts.MAX_VALUE;}
然后写了个测试类,实验了下
public class UnitTest {@Testpublic void testDouble(){double d=0.00/20.00; System.out.println(d+"是否为有限"+Double.isFinite(d));double e=20.00/0.00;System.out.println(e+"是否为无限:"+Double.isInfinite(e));DecimalFormat df = new DecimalFormat("0.00");//格式化小数 String s = df.format(e);System.out.println(s);System.out.println(df.format(d));}
如果有用到求百分比的,应该判断下是否为无限值,这样会避免一些显示问题;
- 虽然double是原生类,但JVM其实是把它当成引入类使用的.所有输出e为'infinity'