Acabo de escribir un pequeño método para contar el número de páginas de SMS del teléfono celular. No tuve la opción de redondear usando Math.ceil
, y honestamente parece ser muy feo.¿Cómo redondear la división de enteros y tener el resultado int en Java?
Aquí está mi código:
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String message = "today we stumbled upon a huge performance leak while optimizing a raycasting algorithm. Much to our surprise, the Math.floor() method took almost half of the calculation time: 3 floor operations took the same amount of time as one trilinear interpolation. Since we could not belive that the floor-method could produce such a enourmous overhead, we wrote a small test program that reproduce";
System.out.printf("COunt is %d ",(int)messagePageCount(message));
}
public static double messagePageCount(String message){
if(message.trim().isEmpty() || message.trim().length() == 0){
return 0;
} else{
if(message.length() <= 160){
return 1;
} else {
return Math.ceil((double)message.length()/153);
}
}
}
realmente no me gusta este trozo de código y estoy buscando una forma más elegante de hacer esto. Con esto, estoy esperando 3 y no 3.0000000. ¿Algunas ideas?
posible duplicado de [Cómo redondear el resultado de la división de enteros] (http: // stackoverflow.com/questions/17944/how-to-round-up-the-result-of-integer-division) – Raedwald