Skip to content
round_to_int

round_to_int

Mojo function 🡭

round_to_int

fn def round_to_int(value: Float64) -> Int

Round-half-away-from-zero, the rounding every primitive here applies when it takes a sub-pixel coordinate to a whole pixel. Float64->Int truncates toward zero, so negative values need the opposite offset from positive ones to round correctly (e.g. -2.5 must become -3, not -2).

Not the stdlib’s round, which is round-half-to-even (2.5 -> 2, 3.5 -> 4): that rule puts evenly spaced half-pixel coordinates on unevenly spaced pixels, so a shape drawn at x = 2.5 and its neighbor at 3.5 would sit two pixels apart instead of one. Replacing this with Int(round(x)) changes where things land.

Public because a caller laying out in Float64 and drawing through the Int-taking primitives (fill_rect, fill_circle_aa) needs the same rounding the package uses, so the two agree on which pixel a coordinate lands in.

Args:

  • value (Float64): The coordinate to round.

Returns:

Int: The nearest integer, halves rounded away from zero.