How to check if a double value has no decimal part

Ankit picture Ankit · Apr 12, 2013 · Viewed 126.4k times · Source

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?

Answer

Lone nebula picture Lone nebula · Apr 12, 2013

You could simply do

d % 1 == 0

to check if double d is a whole.