Simply Maya User Community

Simply Maya User Community (https://simplymaya.com/forum/index.php)
-   Programming (https://simplymaya.com/forum/forumdisplay.php?f=32)
-   -   truncate (https://simplymaya.com/forum/showthread.php?t=14231)

Dann 04-11-2004 10:34 PM

truncate
 
Hey.

Does anyone know how to trucate a number with some degree of control? My float values often look like 0.5505370157 and when I print them out, I'd like to just print 0.55. Maya's trunc command would only print out 0, so that's no good. Maybe a way to round the value off?

Thanks for any thoughts.

-dann

Nimmel 05-11-2004 01:14 AM

Here is a script for you called roundoff. After saving this to your scripts directory etc. run it with 2 inputs, first the number to truncate and second the number of decimal places you want to truncate it to.

e.g.

roundoff 1.2586541 2

// Result: 1.26 //



/////////////////////////////////////////////////////////////////////////
global proc float roundoff( float $f, int $n )
{
if( $n > 0 )
{
float $roundScale = pow(10,$n);
if( $f > 0 )
return( ((float)(int)($f * $roundScale + 0.5)) /$roundScale );
else
return( ((float)(int)($f * $roundScale - 0.5)) /$roundScale );
}
else
{
float $roundScale = pow(10,-$n);
if( $f > 0 )
return( ((float)(int)($f/$roundScale + 0.5)) *$roundScale );
else
return( ((float)(int)($f/$roundScale - 0.5)) *$roundScale );

}
}
/////////////////////////////////////////////////////////////////////////

Dann 05-11-2004 01:40 AM

Thanks. I wrote something similar, but was hoping there was a native Maya command I was overlooking. However, your code is nicer than mine, so I'm gonna use it. Thanks again.


All times are GMT. The time now is 10:48 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Simply Maya 2018