+ 3
In Java, how do we make a type double round UP to the nearest integer?
a= (b/ c); If b is a double, (eg. 5.000053631), how would I be able to cast it to an integer type, with a value of 6 with and without the use of rounding methods, like ceil()? I would need this to work no matter what the value of b may be. 1.3 should round to 2, 12345.22222 should round to 12346, 5678.0987 should round to 5679, etc...
6 Answers
+ 3
Math.round(); function rounds to the nearest integer.
Math.ceil(); function rounds to nearest higher integer
Math.floor(); rounds to nearest lower integer
I hope that my answer helps you...
+ 2
Without using Math.ceil, something like this could work:
int ceil = (int) ((x > (int) x) ? x+1 : x);
https://code.sololearn.com/cS4ygbflwW4T/?ref=app
+ 2
Martin Taylor casting a float number to integer number will round up for negative numbers
and round down for positive numbers.
Edit: it seems you have edited your answer
+ 1
Martin Taylor sorry for the wrong information
+ 1
double a=12345.2222;
int s1=(int)Math.ceil(a);
int s2=(int)Math.floor(a);
int s3=(int)Math.round(a);
//s1=12346
//s2 & s3 =12345
0
In order for you to able to call that back youâd have to add a math.round function)