106

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?

0

8 Answers 8

302

You could simply do

d % 1 == 0

to check if double d is a whole.

Sign up to request clarification or add additional context in comments.

5 Comments

this Double.MIN_VALUE % 1 == 0 gives false
@Evgeniy After all, Double.MIN_VALUE isn't whole.
But modulo does only work with integer, doesn't it?
This solution is not Sonar compliant. Better to use relational operator e.g. less than (<) or greater than (>). For example: d % 1.0 > 0
@HéloïseChauvel What does "Sonar compliant" mean?
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");

4 Comments

You should probably use != rather than >, since it will return false for negative values d.
it does not work with -11111111111111.1;
dose not work with Repeating or Endless Decimals such as Pi or 1/7.
@minchaej Where do you have endless decimals from? I don't know any language where Pi isn't a define and "1/7" is a fraction and not a decimal number.
13

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

if(d % 1 == 0)

Comments

13

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

1 Comment

What about Math.round(x) == x where x a double typed expression.
2

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

Comments

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/

Comments

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));
}

1 Comment

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 stackoverflow.com/a/43656339/3167374, please.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.