Thread: truncate
View Single Post
# 2 05-11-2004 , 01:14 AM
Registered User
Join Date: Aug 2004
Posts: 24
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 );

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


----------------------------------------------
Richard Cheek
https://www.freelance-animation.com
----------------------------------------------