#How do I properly round a float?

6 messages · Page 1 of 1 (latest)

viscid scroll
#

So I know you can round to 3 numbers after comma with %.3f but now I want it to only show these numbers after the comma if there are not 0.

How can I do that?

limber hemlock
viscid scroll
#

yes, but how does that solve my problem?

#

return ("%.3f" % number).strip_edges(true, true) + "e" + str(exponent)

I tried this but it only printed with the .000

limber hemlock
#

%.3f will ensure that the 3 numbers after the comma are there.
How about replacing %.3f with %s if it is a whole number? That would give it to ya normally.

viscid scroll
#

func clean_numstr(str: String) -> String:
var i = str.length() - 1
while i >= 0 and str[i] == "0":
i -= 1
if i >= 0 and str[i] == ".":
i -= 1
return str.substr(0, i + 1)

return clean_numstr("%.3f" % number) + "e" + str(exponent)

This worked for me