Search code examples
julianumerical-methods

How to round to the next largest integer in Julia


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?


Solution

  • 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