+1 vote

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?

in Engine by (100 points)

3 Answers

+2 votes

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

var a = "%.2f" % 8.56239
print(a)
by (16,664 points)
+3 votes

You can also use stepify (link to docs)

var s = stepify(8.56239, 0.01)

by (1,511 points)

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...

Thanks.. I will never knew the function existed otherwise.

Looks like stepify isn't in Godot 4

+1 vote

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

by (371 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.