나는 방금 휴대폰 SMS 용 페이지 수를 세는 작은 방법을 썼다. 나는 Math.ceil
을 사용하여 반올림하는 옵션을 가지고 있지 않았으며 솔직히 매우 추한 것 같습니다. 나는이 코드 조각처럼 정말하지 않습니다정수 나누기를 반올림하고 int 결과를 Java로 작성하는 방법은 무엇입니까?
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);
}
}
}
나는이 일을 더 우아한 방법을 찾고 있어요 :
여기 내 코드입니다. 이것으로, 나는 3.0000000이 아니라 3을 기대하고있다. 어떤 아이디어?
[정수 나누기 결과를 올림하는 방법] 가능한 복제본 (http : // stackoverflow.com/questions/17944/정수법 결과의 반올림) – Raedwald