+ 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...

21st Sep 2021, 12:38 AM
Solus
Solus - avatar
9 Respostas
+ 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...
21st Sep 2021, 4:16 AM
purveshKolhe
purveshKolhe - avatar
+ 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
21st Sep 2021, 5:49 AM
Tibor Santa
Tibor Santa - avatar
+ 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
21st Sep 2021, 5:56 PM
Mehran
Mehran - avatar
+ 1
Martin Taylor sorry for the wrong information
21st Sep 2021, 1:48 AM
Pariket Thakur
Pariket Thakur - avatar
+ 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
21st Sep 2021, 4:11 AM
Patel
0
In order for you to able to call that back you’d have to add a math.round function)
21st Sep 2021, 10:57 PM
Eddie Amerson Jr.
Eddie Amerson Jr. - avatar