Round to next largest integer in Julia?

Viewed 70

If I have some number in Julia like 1.1, is there any function/way to round this Float64 to the next largest integer? For example, what function/method could round 1.1 to 2?

2 Answers

I guess you want smallest integer greater or equal than your value. In this case use:

julia> ceil(Int, 1.1)
2

ceil is probably the preferred method here, but just for completeness, you can pass in a RoundUp RoundingMode to round() and get the nearest larger integer as a float too:

julia> round(1.1, RoundUp)
2.0

You can also search for other RoundingModes by searching for it in help:

help?> RoundingMode
search: RoundingMode

  RoundingMode

  A type used for controlling the rounding mode of floating point operations (via rounding/setrounding functions), or as optional arguments for rounding
  to the nearest integer (via the round function).

  Currently supported rounding modes are:

    •  RoundNearest (default)

    •  RoundNearestTiesAway

    •  RoundNearestTiesUp

    •  RoundToZero

    •  RoundFromZero (BigFloat only)

    •  RoundUp

    •  RoundDown
Related