Is there any method or quick way to check whether a number is an Integer (belongs to Z field) in Java?
I thought of maybe subtracting it from the rounded number, but I didn't find any method that will help me with this.
Where should I check? Integer Api?
if you're talking floating point values, you have to be very careful due to the nature of the format.
the best way that i know of doing this is deciding on some epsilon value, say, 0.000001f, and then doing something like this:
then
essentially you're checking to see if z and the integer case of z have the same magnitude within some tolerance. This is necessary because floating are inherently imprecise.
NOTE, HOWEVER: this will probably break if your floats have magnitude greater than
Integer.MAX_VALUE
(2147483647), and you should be aware that it is by necessity impossible to check for integral-ness on floats above that value.change x to 1 and output is integer, else its not an integer add to count example whole numbers, decimal numbers etc.
One example more :)
In this example, ceil can be used and have the exact same effect.
Check if ceil function and floor function returns the same value