How to round off a number to two decimal places?

69 views
Skip to first unread message

QUAH KAH CHUN UPSI

unread,
Jan 2, 2021, 5:23:02 AM1/2/21
to Glowscript Users
g=9.81
t=1.5291

T="{:.2f}".format(t)

h="{:.2f}".format(0.5*g*T**2)

print(h)


I want to convert  t into two decimal places before bringing into the formula of free fall. 

Bruce Sherwood

unread,
Jan 2, 2021, 12:08:57 PM1/2/21
to Glowscript Users
What you have done is calculate a string variable T and then tried to use T in a numerical expression, 0.5*g*T**2, which can't work.

If you simply want to change t = 1.5291 into t2 = 1.53, a simple way to do that is t2 = round(100*t).

I'll comment that Python now offers what I think is a much more convenient way to create strings that include numbers. Instead of saying "There are {:.2f} seconds".format(t) you say T = f" There are {t:.2f} seconds ". See


Bruce
Reply all
Reply to author
Forward
0 new messages