Round number float – double to int – long Java example

int nf = Math.round(5.789f);
System.out.print(nf); // 6
 
float f = 28.611f; 
int n3 = Math.round(f);
System.out.println(n3); // 29
 
double d = 1234.56;
long lon = Math.round(d);
System.out.println(lon); // 1235
 
int diff = 90 - 40;
// float fDeleni = diff / 10; // error code
float fDeleni = (float)diff / 10.f; // ok
int nRound = Math.round(fDeleni);
 
 
// Caution:
int n2 = (int) 8.999f;
System.out.println(n2); // 8
This entry was posted in Android Examples Code and tagged , . Bookmark the permalink.


Comments are closed.

9,570 views