Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Rounding a float to the second place?

Author
Message
Ryeguy457
21
Years of Service
User Offline
Joined: 5th Oct 2002
Location:
Posted: 13th Oct 2002 09:22
How can I round a float number to the second place? Like if I had 126.6261, how would I round it to 126.63?
Zed
21
Years of Service
User Offline
Joined: 20th Sep 2002
Location:
Posted: 13th Oct 2002 11:49
One way would be to convert it to a string, find the '.' then count 2 more digits. Convert your string back into a real value having ignored the extra digits. Now check the next digit in the string to see if its higher than 4 and if it is add 0.01 to the real value.

swdave
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: United States
Posted: 13th Oct 2002 11:50
126.6261 * 100 = 12662.61
int(12662.61) = 12662
12662 / 100 = 126.62

Zed
21
Years of Service
User Offline
Joined: 20th Sep 2002
Location:
Posted: 13th Oct 2002 11:50
Oh and the quicker way is to multiply the number by 100 then add 0.5 then get the int() of the number and divide this by 100.

swdave
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: United States
Posted: 13th Oct 2002 12:08
oops

126.6261 * 100 = 12662.61
int(12662.61) = 12662
12662 / 100 = 126.62
if 126.6261 - 126.62 >= 0.005 then add 0.01

a# = 126.6261
b# = a# * 100
i = int(b#)
c# = i / 100
if a# - c# >= 0.005 then c# = c# + 0.01

Login to post a reply

Server time is: 2024-04-19 14:58:31
Your offset time is: 2024-04-19 14:58:31