How can i check number is divisible by particular number or not both numbers are decimal. with decimal point value of two digits.
i had tried with below
(((dn / size) % 1) == 0)
but in some cases it not provide proper out put.
how can i resolve it. here i put some example values like
double dn = 369.35,369.55.370.55
and size may be 0.05,0.10,0.5
etc...
if(((dn / size) % 1) == 0) {
Log.d(TAG,"OK");
} else {
Log.d(TAG,"OK");
}
please help me to short out it.
You are doing it wrongly,
try with this code
Update
Use BigDecimal in case you have big decimal numbers
eg :
(dn / size) % 1 == 0
whilst plausible, will suffer from pitfalls centred around binary floating point arithmetic.If your numbers always have no more then two decimal places then the easiest thing to do is scale up by multiplying by 100 and rely on the fact that
100 * y
divides100 * x
ify
dividesx
. In other words:This obviates any issues with binary floating point arithmetic. I've used
Math.round
rather than a truncation deliberately, again to obviate issues around floating point and am relying on what is in my opinion a quirk of this platform in thatround
returns an integral type.Further reading: Is floating point math broken?