मैं सिर्फ सेल फोन एसएमएस के लिए पृष्ठों की संख्या गिनती करने के लिए एक छोटे से विधि लिखा था। मेरे पास Math.ceil
का उपयोग करके गोल करने का विकल्प नहीं था, और ईमानदारी से यह बहुत बदसूरत प्रतीत होता है।पूर्णांक विभाजन को कैसे पूर्ण करें और जावा में int परिणाम प्राप्त करें?
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 की उम्मीद कर रहा हूं और 3.0000000 नहीं। कोई विचार?
[पूर्णांक डिवीजन के परिणाम को ख़त्म कर कैसे] (http के संभावित डुप्लिकेट: // stackoverflow।कॉम/प्रश्न/17 9 44/कैसे-टू-राउंड-अप-द-परिणाम-का-पूर्णांक-विभाजन) – Raedwald