# How to round to a specific decimal place?

Attention Topic was automatically imported from the old Question2Answer platform.

I want to round 1.3453 to 1.35.
round() only rounds to the nearest decimal. Some languages have a second parameter in the round() function to designate what position to round to. Is there an equivalent in Godot?

I couldn’t find anything in the godot library that does this, you could easily make your own. Try this, I just whipped it up →

func round_place(num,places):
return (round(num*pow(10,places))/pow(10,places))

where num is your number and places is the number of decimal places you want rounded

Hey! That’s the same as my answer. >:|

SIsilicon | 2018-06-02 04:45

Yeah when i posted this your answer wasnt shown yet. I dont think answers get posted right away as i was confused why yours has a date posted earlier than mine yet i couldnt see it at the time. Your post is much cleaner though

happycamper | 2018-07-13 14:01

Is there an equivalent in Godot?

No as far as I can see…
BUT! You can easily make a function like that.

``````func round_to_dec(num, digit):
return round(num * pow(10.0, digit)) / pow(10.0, digit)
``````

`digit` must be an integer.

`digit` > 0 rounds `num` with more digits.

``````round_to_dec(1.352, 2) #rounds to the 2nd decimal digit. 1.35
``````

`digit` < 0 rounds `num` with less digits.

``````round_to_dec(24.352, -2) #rounds to the 2nd integer digit. 20
``````

`digit` = 0 makes a regular round function.

So, there actually is something in Godot already. It is the function “stepify()”

stepify(1.3453,0.01)

returns 1.35

for anyone stumbling across this in 2023, stepify() has been renamed to snapped() in godot4.

kiril | 2023-05-31 02:15

1 Like
``````func round_to_dec(num, decimals):
num = float(num)
decimals = int(decimals)
var sgn = 1
if num < 0:
sgn = -1
num = abs(num)
pass
var num_fraction = num - int(num)
var num_dec = round(num_fraction * pow(10.0, decimals)) / pow(10.0, decimals)
var round_num = sgn*(int(num) + num_dec)
return round_num
pass

``````

I created this function. Now you can tell how many decimals the number must be rounded.

2020 Solution

3.1 Has format strings which makes this a lot easier

`"%.3f" % (10.22)` 10.220

`"%.3f" % (10.2205)` 10.221

`"%.3f" % (10)` 10.000