Java中如何对整数除法进行四舍五入并得到int结果?
我刚刚写了一个小方法来计算手机短信的页数。我没有使用 Math.ceil
进行四舍五入的选项,老实说,它看起来非常丑陋。
这是我的代码:
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。有什么想法吗?
I just wrote a tiny method to count the number of pages for cell phone SMS. I didn't have the option to round up using Math.ceil
, and honestly it seems to be very ugly.
Here is my code:
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);
}
}
}
I don't really like this piece of code and I'm looking for a more elegant way of doing this. With this, I'm expecting 3 and not 3.0000000. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
使用Math.ceil() 并将结果转换为int:
示例:
Use
Math.ceil()
and cast the result to int:Example:
要对整数除法进行四舍五入,您可以使用
or 如果两个数字都是正数
To round up an integer division you can use
or if both numbers are positive
另一种不太复杂的单行:
可以使用 long 而不是 int;只需更改参数类型和返回类型即可。
Another one-liner that is not too complicated:
Could use long instead of int; just change the parameter types and return type.
Google 的 Guava 库在 IntMath 类中处理此问题:
与此处的许多答案不同,它处理负数。当尝试除以零时,它还会引发适当的异常。
Google's Guava library handles this in the IntMath class:
Unlike many answers here, it handles negative numbers. It also throws an appropriate exception when attempting to divide by zero.
这将给出一个“向上舍入”的整数。
This will give a "rounded up" integer.
如果你想计算 a 除以 b 向上取整,你可以使用 (a+(-a%b))/b
If you want to calculate a divided by b rounded up you can use (a+(-a%b))/b
扩展彼得的解决方案,我发现这对我来说总是可以围绕“正无穷大”:
Expanding on Peter's solution, this is what I've found works for me to always round 'towards positive infinity':
这可能会有所帮助,,
将余数减去长度,使其成为可整除的数,然后除以 153
this might be helpfull,,
Subtract the remainder to the legnth and make it a divisible number and then divide it with 153