Skip to content

Instantly share code, notes, and snippets.

@ATMarcks
Created April 30, 2018 02:38
Show Gist options
  • Select an option

  • Save ATMarcks/aa34c67b7a5226c4fb74edbacb102c9d to your computer and use it in GitHub Desktop.

Select an option

Save ATMarcks/aa34c67b7a5226c4fb74edbacb102c9d to your computer and use it in GitHub Desktop.
Java round double
public class ExcellentRounding {
public static void main(String []args){
double x = -23.1;
System.out.println(x);
int rounded = round(x);
System.out.println(rounded);
}
public static int round(double x) {
if (~((int)x >> 31) == 0 ? false : true) {
if (((int)(x * 10) % 10) >= 5) {
return (int)(x + 1);
} else {
return (int)(x);
}
} else {
if ((~((int)(x * 10)) % 10) + 1 >= 5) {
return (int)(x - 1);
} else {
return (int)(x);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment