98

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.

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

What could be the best way of doing this?

Ankit
  • 4,426
  • 7
  • 45
  • 62

8 Answers8

290

You could simply do

d % 1 == 0

to check if double d is a whole.

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Lone nebula
  • 4,768
  • 2
  • 16
  • 16
25
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");
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
13

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

if(d % 1 == 0)
Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126
12

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
Ankit
  • 6,554
  • 6
  • 49
  • 71
2

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

Patashu
  • 21,443
  • 3
  • 45
  • 53
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/

Floaf
  • 145
  • 7
1

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.

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));
}
stivlo
  • 83,644
  • 31
  • 142
  • 199
  • `double` can exactly represent up to 53 bits, so there's no need for tolerance for current definition of `int` data type in java, which is strictly 32 bit. If the original value was converted from int to double, then it can be converted back exactly, without any loss of precision. See https://stackoverflow.com/a/43656339/3167374, please. – shitpoet Nov 26 '19 at 09:04
0

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

Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33