在java中获取下一个更高的整数值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23246983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:30:30  来源:igfitidea点击:

Get the next higher integer value in java

javamathnumbers

提问by Wearybands

I know I can use Math.java functions to get the floor, ceil or round value for a double or float, but my question is -- Is it possible to always get the higher integer value if a decimal point comes in my value

我知道我可以使用 Math.java 函数来获取双精度或浮点数的下限、上限或舍入值,但我的问题是——如果小数点出现在我的值中,是否可以始终获得更高的整数值

For example

例如

int chunkSize  = 91 / 8 ; 

which will be equal to 11.375

这将等于 11.375

If I apply floor, ceil or round to this number it will return 11 I want 12.

如果我对这个数字应用 floor、ceil 或 round,它将返回 11 我想要 12。

Simply If I have 11.xxx I need to get 12 , if I have 50.xxx I want 51

如果我有 11.xxx 我需要 12 ,如果我有 50.xxx 我想要 51

Sorry The chunkSize should be int

抱歉 chunkSize 应该是 int

How can I achieve this?

我怎样才能做到这一点?

采纳答案by BobTheBuilder

Math.ceil()will do the work.

Math.ceil()会做的工作。

But, your assumption that 91 / 8is 11.375is wrong. In java, integer division returns the integervalue of the division (11 in your case).

但是,你的假设91 / 811.375错了。在 java 中,整数除法返回除法的数值(在您的情况下为 11)。

In order to get the float value, you need to cast (or add .0) to one of the arguments:

为了获得浮点值,您需要将(或添加 .0)到参数之一:

float chunkSize  = 91 / 8.0 ;
Math.ceil(chunkSize); // will return 12!

回答by Ivaylo Strandjev

ceilis supposed to do just that. I quote:

ceil应该做到这一点。我引用:

Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.

返回大于或等于参数且等于数学整数的最小(最接近负无穷大)双精度值。

EDIT: (thanks to Peter Lawrey): taking another look at your code you have another problem. You store the result of integer division in a float variable: float chunkSize = 91 / 8 ;java looks at the arguments of the division and as they are both integers it performs integer division thus the result is again an integer(the result of the division rounded down). Now even if you assign this to the float chunkSizeit will still be an integer(missing the double part) and ceil, round and floor will all return the same value. To avoid that add a .0to 91: float chunkSize = 91.0 / 8;. This makes one of the arguments double precision and thus double division will be performed, returning a double result.

编辑:(感谢 Peter Lawrey):再看看你的代码,你还有另一个问题。您将整数除法的结果存储在一个浮点变量中:float chunkSize = 91 / 8 ;java 查看除法的参数,因为它们都是整数,所以它执行整数除法,因此结果又是一个整数(除法的结果向下取整)。现在,即使您将chunkSize它分配给浮点数,它仍然是一个整数(缺少双精度部分)并且 ceil、round 和 floor 都将返回相同的值。为避免这种情况,请.091:中添加一个float chunkSize = 91.0 / 8;。这使参数之一成为双精度,因此将执行双除法,返回双精度结果。

回答by Peter Lawrey

If you want to do integer division rounding up you can do

如果你想做整数除法四舍五入你可以做

x / yrounded up, assuming x and y are positive

x / y向上取整,假设 x 和 y 为正

long div = (x + y - 1) / y;

In your case

在你的情况下

(91 + 8 - 1) / 8 = 100 / 8 = 12

This works because you want to round up (add one) for every value except an exact multiple (which is why you add y - 1)

这是有效的,因为您想为除精确倍数之外的每个值四舍五入(加一)(这就是您添加的原因y - 1

回答by Aman

Firstly, when you write float chunkSize = 91 / 8 ;it will print 11.0 instead of 11.375

首先,当你写的float chunkSize = 91 / 8 ;时候会打印 11.0 而不是 11.375

because both 91 and 8 are int type values. For the result to be decimal value, either dividend or divisor

因为 91 和 8 都是 int 类型值。对于十进制值的结果,被除数或除数

or both have to be decimal value type. So, float chunkSize = 91.0 / 8;will result 11.375 and

或者两者都必须是十进制值类型。因此,float chunkSize = 91.0 / 8;将导致 11.375 和

now you can apply Math.ceil() to get the upper value. Math.ceil(chunkSize);will result in 12.0

现在您可以应用 Math.ceil() 来获得上限值。Math.ceil(chunkSize);将导致 12.0