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

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




PostPosted: Thu Jan 06, 2005 6:36 pm   Post subject: Rounding

is ther any way to make a prcedure that rounds a number to the nearcest digit??
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Thu Jan 06, 2005 6:47 pm   Post subject: (No subject)

To the nearest digit? Umm... round(r : real) : int
xHoly-Divinity




PostPosted: Thu Jan 06, 2005 7:01 pm   Post subject: (No subject)

use :1:n

e.g.

put 53.423:1:2 <-- that will round to 2 decimal places
put 53.424:1:3 <-- that will round to 3 decimal places

etc. etc. etc.
Cervantes




PostPosted: Thu Jan 06, 2005 7:08 pm   Post subject: (No subject)

That will work if you want to just put the rounded number on the screen in standard text. But, what if you want to round the number and store it as a variable?
Martin




PostPosted: Thu Jan 06, 2005 7:30 pm   Post subject: (No subject)

code:
fcn Round (n : real, accuracy: int) : real
    %accuracy is the number of decimal places you want it to be rounded to
    %for example, to get 4.5 out of 4.43, you'd have accuracy = 1.
    %to get 4 out of 4.33, you'd have accuracy = 0
    return round  ((10**accuracy) * n)) / (10**accuracy)
end round
Genesis




PostPosted: Tue Feb 15, 2005 1:48 am   Post subject: (No subject)

That doesn't work for me. I changed it to this:

code:
fcn Round (n : real, accuracy: int) : real
    %accuracy is the number of decimal places you want it to be rounded to
    %for example, to get 4.5 out of 4.43, you'd have accuracy = 1.
    %to get 4 out of 4.33, you'd have accuracy = 0
    result round  ((10**accuracy) * n) / (10**accuracy)
end Round


(Changed "Return" to "Result" and removed a bracket.)
Delos




PostPosted: Tue Feb 15, 2005 10:43 am   Post subject: (No subject)

Or you could use the floor() or ceil() commands.
Drakain Zeil




PostPosted: Tue Feb 15, 2005 5:00 pm   Post subject: (No subject)

For rounding up or down anyway.
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: