PDA

View Full Version : Rounding values in MEL ?


ChrisMillar
04-05-2010, 10:56 AM
Hello,

say I have:

float $pos[3];

at the moment I'm rounding the values as follows:

(floor ($pos[0] + 0.5 )
(floor ($pos[1] + 0.5 )
(floor ($pos[2] + 0.5 )

That gives me int's but I want say 3 decimal places and the last value rounded correctly - any tips ?

Also, what is the syntax so I dont have to type out the line three times ?

(floor ($pos[] + 0.5 ) - didn't work like I thought it might

What are the tricks ?

Kind regards ;)

NaughtyNathan
04-05-2010, 11:08 AM
this has come up several times before:

http://forums.cgsociety.org/showthread.php?f=89&t=855181&highlight=round+mel

search is your friend..

claudiof
04-05-2010, 08:39 PM
global proc int round(float $ANum)
{
float $n = $ANum % 1;

if ($n >= 0.5) {
$ANum = `ceil $ANum`;
} else {
$ANum = `floor $ANum`;
}
return $ANum;

}
//round(3.4); result:3
//round(3.6); result: 4

CGTalk Moderation
04-05-2010, 08:39 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.