Round Float To Next Integer

Godot Version

4.2.2

Question

so i am trying to round a float to integer and i am trying to achieve something like this:

if value is 0.4444 it should round to 0 but if value is 0.6666 it should round to 1

if a value is -1.4 it rounds up to 0 too but if its -1.6 it rounds up to -2

how can i achieve that?

We can use:

var rounded_value : int = roundf(-1.6)

More info in the manual here:

1 Like

This doesn’t make any sense. Do you mean that round(-1.4) == -1 and round(-1.6) == -2?

It will be quite tricky to get it o round -1.4 to ZERO which is what you have written.

I’m going to assume that you mean -1 rather than 0.

And that is already the result you will get with both round and snapped.

extends Node

var a = -1.4
var b = -1.6

func _ready():
    prints(round(a), round(b))
    prints(snapped(a, 1), snapped(b, 1))

outputs:

-1 -2
-1 -2
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.