finity和infinity

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 {
  @Test
  public 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'

转载于:https://my.oschina.net/u/1590027/blog/718351

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值