如何检查双值是否没有十进制[重复]

时间:2022-06-21 16:34:35

This question already has an answer here:

这个问题已经有了答案:

I have a double value which I have to display at my UI. Now the condition is that the decimal value of double = 0 eg. - 14.0 In that case I have to show only 14 on my UI. Also, the max limit for characters is 5 here.

我有一个双值,必须在UI中显示。现在的条件是二重= 0。- 14。0在这种情况下,我只能在UI上显示14。另外,字符的最大限制是5。

eg.- 12.34 the integer value can be no bigger than 2 digits and so is the decimal value for our double.

如。- 12.34整数值可以不大于2位,因此,我们的双精度值也不大于2位。

What could be the best way of doing this?

最好的办法是什么?

8 个解决方案

#1


142  

You could simply do this: d % 1 == 0 to see if some double d is whole.

你可以这样做:d % 1 = 0,看看某个双d是否完整。

#2


14  

double d = 14.4;
if((d-(int)d)!=0)
    System.out.println("decimal value is there");
else
    System.out.println("decimal value is not there");

#3


7  

All Integers are modulo of 1. So below check must give you the answer.

所有的整数都是1的模。所以下面的检查一定会给你答案。

if(d % 1 == 0)

#4


6  

either ceil and floor should give the same out out put

无论是西伊尔还是地板都应该给同样的输出

Math.ceil(x.y) == Math.floor(x.y)

or simply check for equality with double value

或者简单地检查具有双重值的等式

x.y == Math.ceil(x.y)
x.y == Math.floor(x.y)

or

Math.round(x.y) == x.y

#5


2  

Compare two values: the normal double, and the double after flooring it. If they are the same value, there is no decimal component.

比较两个值:正常的双,地板后的双。如果它们是相同的值,则没有十进制分量。

#6


1  

Use number formatter to format the value, as required. Please check this.

根据需要,使用数字格式化程序来格式化值。请检查这个。

#7


1  

You probably want to round the double to 5 decimals or so before comparing since a double can contain very small decimal parts if you have done some calculations with it.

在比较之前,你可能想把小数点四舍五入,因为如果你对它做过一些计算的话,双精度可以包含很小的小数部分。

double d = 10.0;
d /= 3.0; // d should be something like 3.3333333333333333333333...
d *= 3.0; // d is probably something like 9.9999999999999999999999...

// d should be 10.0 again but it is not, so you have to use rounding before comparing

d = myRound(d, 5); // d is something like 10.00000
if (fmod(d, 1.0) == 0)
  // No decimals
else
  // Decimals

If you are using C++ i don't think there is a round-function, so you have to implement it yourself like in: http://www.cplusplus.com/forum/general/4011/

如果您正在使用c++,我认为不存在循环函数,所以您必须自己实现它,如http://www.cplusplus.com/forum/general/4011/

#8


0  

Interesting little problem. It is a bit tricky, since real numbers, not always represent exact integers, even if they are meant to, so it's important to allow a tolerance.

有趣的小问题。这有点棘手,因为实数并不总是表示精确的整数,即使它们是命中注定的,所以允许容错是很重要的。

For instance tolerance could be 1E-6, in the unit tests, I kept a rather coarse tolerance to have shorter numbers.

例如公差可以是1E-6,在单元测试中,我保持了一个比较粗的公差,以使数据更短。

None of the answers that I can read now works in this way, so here is my solution:

我现在读到的答案都不是这样的,所以这是我的解决方案:

public boolean isInteger(double n, double tolerance) {
    double absN = Math.abs(n);
    return Math.abs(absN - Math.round(absN)) <= tolerance;
}

And the unit test, to make sure it works:

和单元测试,以确保它工作:

@Test
public void checkIsInteger() {
    final double TOLERANCE = 1E-2;
    assertThat(solver.isInteger(1, TOLERANCE), is(true));

    assertThat(solver.isInteger(0.999, TOLERANCE), is(true));
    assertThat(solver.isInteger(0.9, TOLERANCE), is(false));

    assertThat(solver.isInteger(1.001, TOLERANCE), is(true));
    assertThat(solver.isInteger(1.1, TOLERANCE), is(false));

    assertThat(solver.isInteger(-1, TOLERANCE), is(true));

    assertThat(solver.isInteger(-0.999, TOLERANCE), is(true));
    assertThat(solver.isInteger(-0.9, TOLERANCE), is(false));

    assertThat(solver.isInteger(-1.001, TOLERANCE), is(true));        
    assertThat(solver.isInteger(-1.1, TOLERANCE), is(false));
}

#1


142  

You could simply do this: d % 1 == 0 to see if some double d is whole.

你可以这样做:d % 1 = 0,看看某个双d是否完整。

#2


14  

double d = 14.4;
if((d-(int)d)!=0)
    System.out.println("decimal value is there");
else
    System.out.println("decimal value is not there");

#3


7  

All Integers are modulo of 1. So below check must give you the answer.

所有的整数都是1的模。所以下面的检查一定会给你答案。

if(d % 1 == 0)

#4


6  

either ceil and floor should give the same out out put

无论是西伊尔还是地板都应该给同样的输出

Math.ceil(x.y) == Math.floor(x.y)

or simply check for equality with double value

或者简单地检查具有双重值的等式

x.y == Math.ceil(x.y)
x.y == Math.floor(x.y)

or

Math.round(x.y) == x.y

#5


2  

Compare two values: the normal double, and the double after flooring it. If they are the same value, there is no decimal component.

比较两个值:正常的双,地板后的双。如果它们是相同的值,则没有十进制分量。

#6


1  

Use number formatter to format the value, as required. Please check this.

根据需要,使用数字格式化程序来格式化值。请检查这个。

#7


1  

You probably want to round the double to 5 decimals or so before comparing since a double can contain very small decimal parts if you have done some calculations with it.

在比较之前,你可能想把小数点四舍五入,因为如果你对它做过一些计算的话,双精度可以包含很小的小数部分。

double d = 10.0;
d /= 3.0; // d should be something like 3.3333333333333333333333...
d *= 3.0; // d is probably something like 9.9999999999999999999999...

// d should be 10.0 again but it is not, so you have to use rounding before comparing

d = myRound(d, 5); // d is something like 10.00000
if (fmod(d, 1.0) == 0)
  // No decimals
else
  // Decimals

If you are using C++ i don't think there is a round-function, so you have to implement it yourself like in: http://www.cplusplus.com/forum/general/4011/

如果您正在使用c++,我认为不存在循环函数,所以您必须自己实现它,如http://www.cplusplus.com/forum/general/4011/

#8


0  

Interesting little problem. It is a bit tricky, since real numbers, not always represent exact integers, even if they are meant to, so it's important to allow a tolerance.

有趣的小问题。这有点棘手,因为实数并不总是表示精确的整数,即使它们是命中注定的,所以允许容错是很重要的。

For instance tolerance could be 1E-6, in the unit tests, I kept a rather coarse tolerance to have shorter numbers.

例如公差可以是1E-6,在单元测试中,我保持了一个比较粗的公差,以使数据更短。

None of the answers that I can read now works in this way, so here is my solution:

我现在读到的答案都不是这样的,所以这是我的解决方案:

public boolean isInteger(double n, double tolerance) {
    double absN = Math.abs(n);
    return Math.abs(absN - Math.round(absN)) <= tolerance;
}

And the unit test, to make sure it works:

和单元测试,以确保它工作:

@Test
public void checkIsInteger() {
    final double TOLERANCE = 1E-2;
    assertThat(solver.isInteger(1, TOLERANCE), is(true));

    assertThat(solver.isInteger(0.999, TOLERANCE), is(true));
    assertThat(solver.isInteger(0.9, TOLERANCE), is(false));

    assertThat(solver.isInteger(1.001, TOLERANCE), is(true));
    assertThat(solver.isInteger(1.1, TOLERANCE), is(false));

    assertThat(solver.isInteger(-1, TOLERANCE), is(true));

    assertThat(solver.isInteger(-0.999, TOLERANCE), is(true));
    assertThat(solver.isInteger(-0.9, TOLERANCE), is(false));

    assertThat(solver.isInteger(-1.001, TOLERANCE), is(true));        
    assertThat(solver.isInteger(-1.1, TOLERANCE), is(false));
}