简体   繁体   中英

Is there a JDK method for changing a double to the nearest larger int, even if it's already an integer value?

I want to be able to 'round' up any double value to the closest higher integer, even if the value itself is already an integer value.

Math.ceil(2) returns 2 , since 2 is already equal to an integer value. I need a function that would return 3 in this case.

I can easily code this myself but am curious whether this already exists in the JDK.

EDIT: In the likely case that I will just code this myself given how trivial I think this is, please tell me if the following code looks okay to you:

public static double convertToNearestLargerIntValue(double value){
    return Math.floor(value + 1.0);
}

如此简单,不需要JDK方法。

Math.floor(value) + 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM