Computer Science Canada rounding real numbers |
Author: | MysticVegeta [ Fri Mar 18, 2005 5:44 pm ] |
Post subject: | rounding real numbers |
Hi, I want to know how to round a numbers something like this -> 86.888 to 86.89 if i use the index and substrings to do this then if the numbers is something like this -> 89.999 then with my method it wil round it to something like this -> 89.90 lol. Should be 90.00 is there a command for that? or anyway? |
Author: | Bacchus [ Fri Mar 18, 2005 5:57 pm ] |
Post subject: | |
if you want to round t 2 decimal places, multiply your number by 100 and round then divide the number by 100 86.888*100 round(8688.8) 8689.0/100 86.890 |
Author: | Flikerator [ Fri Mar 18, 2005 6:37 pm ] | ||||
Post subject: | |||||
Or if you just want to show two places do this
to
|
Author: | Cervantes [ Sat Mar 19, 2005 11:54 am ] | ||||||
Post subject: | |||||||
Heh, but that only works with put. That may be fine, but let's explore even more possibilities!
This, however, gets us a string. If you want to do any manipulation with the somewhat rounded number, it doesn't help us. The next bit of code does, though.
So, we've got lots of alternatives here. If you want to put the somewhat rounded number on the screen, use Flikerator's method. If you want to put the rounded number on the screen with a Font.Draw, use frealstr. If you want to somewhat round the number and then manipulate it,you can use Bacchus' approach, or my second frealstr example. Bacchus' method is preferred, howver, because you don't have to bother with string manipulation and you don't have to bother with extra variables. It's a one-line conversion :
|
Author: | ssr [ Sat Mar 19, 2005 12:47 pm ] | ||||
Post subject: | |||||
also u can use
one rounds down and one rounds up |
Author: | MysticVegeta [ Tue Mar 22, 2005 6:09 pm ] | ||
Post subject: | |||
This is exactly what i was looking for, and here i was using substrings and indexes to do it in for loops. sigh. I feel like an idiot. Thanks a lot btw |