How to get the first two decimals of a float?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SebSharper

I need to get x decimals of a float, instead of all of them, for example:
8.56239 should be just 8.56
How to achieve this?

:bust_in_silhouette: Reply From: jgodfrey

Assuming you want the value as a string, this should work:

var a = "%.2f" % 8.56239
print(a)
:bust_in_silhouette: Reply From: omggomb

You can also use stepify (link to docs)

var s = stepify(8.56239, 0.01)

Thank you a lot, this one solved my problem. i did use it like this.

if stepify($timer.time_left,0.1) == 0.2:
       do stuff

timer time_left return a float around 1.23456 value like this and i was need of only first two…

morningkingdom | 2021-11-25 18:53

Thanks… I will never knew the function existed otherwise.

molesallegiance | 2022-10-27 13:06

Looks like stepify isn’t in Godot 4

Satscape | 2023-01-24 21:04

:bust_in_silhouette: Reply From: Surtarso

var twodecimals = str( BIGFLOATNUMBER ).pad_decimals(2)

Perfect but i has to be float(str( BIGFLOATNUMBER ).pad_decimals(2)) or int(str( BIGFLOATNUMBER ).pad_decimals(2))

demamolabs | 2021-07-30 19:45