BigDecimal - 检查值在双范围内 [英] BigDecimal - check value is within double range

查看:845
本文介绍了BigDecimal - 检查值在双范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序从某个地方解析一个数字,并检查它是一个有效的int(Integer.MIN_VALUE和Integer.MAX_VALUE之间)或一个有效的double(Double.MIN_VALUE和Double.MAX_VALUE之间)。 p>

我使用这段代码:

  import java.math.BigDecimal ; 
import java.math.BigInteger;

public class Test {

public static final BigDecimal DOUBLE_MAX = BigDecimal.valueOf(Double.MAX_VALUE);
public static final BigDecimal DOUBLE_MIN = BigDecimal.valueOf(Double.MIN_VALUE);

public static final BigInteger INTEGER_MIN = BigInteger.valueOf(Integer.MIN_VALUE);
public static final BigInteger INTEGER_MAX = BigInteger.valueOf(Integer.MAX_VALUE);

private static boolean inRange(BigDecimal value){
return DOUBLE_MAX.compareTo(value)> = 0&&
DOUBLE_MIN.compareTo(value)< = 0;
}

private static boolean inRange(BigInteger value){
return INTEGER_MAX.compareTo(value)> = 0&&
INTEGER_MIN.compareTo(value)< = 0;
}

public static void main(String [] args)
{
System.out.println(inRange(new BigInteger(1234656)));
System.out.println(inRange(new BigInteger(0)));
System.out.println(inRange(new BigInteger( - 987)));

System.out.println(inRange(new BigDecimal(1234656.0)));
System.out.println(inRange(new BigDecimal(0.0)));
System.out.println(inRange(new BigDecimal( - 987.0)));
}
}

哪些适用于int值,但由于某些原因,任何零或负双重值失败。所以运行以上生成输出:

  true 
true
true
true
false
false

我在这里做错什么?



另外,我看到DOUBLE_MIN设置为-Double.MAX_VALUE的示例。这是正确的吗?



谢谢。

解决方案

code> Double.MIN_VALUE 表示最小正值值。 (即,接近零的正值)。



文档


最小的正非零值为double,2 -1074


这是为什么

  System.out.println(inRange(new BigDecimal(0.0))); 
System.out.println(inRange(new BigDecimal( - 987.0)));

输出错误。没有提供的值(严格)大于0。






/ h2>

由于 double 的范围在origo周围是对称的(而不是整数,在负面),您可以通过写入 -Double.MAX_VALUE 获得最小(负)值。那是

  BigDecimal DOUBLE_MIN = BigDecimal.valueOf(-Double.MAX_VALUE); 


I have a Java application which parses a number from somewhere, and checks that it is a valid int (between Integer.MIN_VALUE and Integer.MAX_VALUE) or a valid double (between Double.MIN_VALUE and Double.MAX_VALUE).

I'm using this code:

import java.math.BigDecimal;
import java.math.BigInteger;

  public class Test  {

    public static final BigDecimal DOUBLE_MAX = BigDecimal.valueOf(Double.MAX_VALUE);
    public static final BigDecimal DOUBLE_MIN = BigDecimal.valueOf(Double.MIN_VALUE);

    public static final BigInteger INTEGER_MIN = BigInteger.valueOf(Integer.MIN_VALUE);
    public static final BigInteger INTEGER_MAX = BigInteger.valueOf(Integer.MAX_VALUE);

    private static boolean inRange(BigDecimal value) {
        return DOUBLE_MAX.compareTo(value) >= 0 &&
          DOUBLE_MIN.compareTo(value) <= 0;
    }

    private static boolean inRange(BigInteger value) {
        return INTEGER_MAX.compareTo(value) >= 0 &&
          INTEGER_MIN.compareTo(value) <= 0;
    }

    public static void main(String[] args)
    {
        System.out.println(inRange(new BigInteger("1234656")));
        System.out.println(inRange(new BigInteger("0")));
        System.out.println(inRange(new BigInteger("-987")));

        System.out.println(inRange(new BigDecimal("1234656.0")));
        System.out.println(inRange(new BigDecimal("0.0")));
        System.out.println(inRange(new BigDecimal("-987.0")));
    }
  }

Which works fine for int values, but for some reason, fails for any zero or negative double value. So running the above produces the output:

true
true
true
true
false
false

What am I doing wrong here?

Also, I've seen examples where DOUBLE_MIN is set to be -Double.MAX_VALUE. This works but is it correct?

Thanks.

解决方案

Double.MIN_VALUE represents the minimum positive value. (That is, a positive value close to zero.)

From the documentation:

A constant holding the smallest positive nonzero value of type double, 2-1074.

This is the reason why

System.out.println(inRange(new BigDecimal("0.0")));
System.out.println(inRange(new BigDecimal("-987.0")));

outputs false. None of the provided values are (strictly) greater than 0.


The solution

Since the range of doubles are symmetrical around origo (as opposed to integers, which stretches one step further on the negative side) you can get the minimum (negative) value by writing -Double.MAX_VALUE. That is

BigDecimal DOUBLE_MIN = BigDecimal.valueOf(-Double.MAX_VALUE);

这篇关于BigDecimal - 检查值在双范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆