Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 rounding real numbers
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MysticVegeta




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
Bacchus




PostPosted: Fri Mar 18, 2005 5:57 pm   Post subject: (No 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
Flikerator




PostPosted: Fri Mar 18, 2005 6:37 pm   Post subject: (No subject)

Or if you just want to show two places do this

code:
put 85.543


to

code:
put 85.543 : 0 : 2
Cervantes




PostPosted: Sat Mar 19, 2005 11:54 am   Post subject: (No subject)

Heh, but that only works with put. That may be fine, but let's explore even more possibilities! Very Happy

Turing:

var num := 89.369
put frealstr (num, 0, 2) %0 = width of the string.  For most cases, just leave it at 0
% 2 = the number of decimal places

%or you can Font.Draw it, using frealstr
%Font.Draw (frealstr (num, 0, 2), x, y, fontID, colour)

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.
Turing:

var num := 89.369
var roundedNumString := frealstr (num, 0, 2)
var roundedNum := strreal (roundedNumString)
put roundedNum


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 Wink :
Turing:

var num := 89.369
num := round (num * 100) / 100
put num

ssr




PostPosted: Sat Mar 19, 2005 12:47 pm   Post subject: (No subject)

also u can use
code:
floor
and
code:
ceil

one rounds down and one rounds up Very Happy
MysticVegeta




PostPosted: Tue Mar 22, 2005 6:09 pm   Post subject: (No subject)

code:

var num := 89.369
var roundedNumString := frealstr (num, 0, 2)
var roundedNum := strreal (roundedNumString)
put roundedNum


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 Wink
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: