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?
Compare two values: the normal double, and the double after floor ing it. If they are the same value, there is no decimal component.
function number_test(n) { var result = (n - Math. floor(n)) !== 0; if (result) return 'Number has a decimal place.
var decPlaces = (int)(((decimal)number % 1) * 100);
Truncation Using Casting If our double value is within the int range, we can cast it to an int. The cast truncates the decimal part, meaning that it cuts it off without doing any rounding.
You could simply do
d % 1 == 0
to check if double d
is a whole.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With