claudiof

03-11-2009, 03:34 PM

is there any function can :

round(3.6) //result 4

round(3.4) //result 3

?

round(3.6) //result 4

round(3.4) //result 3

?

View Full Version : ceil - floor is ok.but where is round ?

claudiof 03-11-2009, 03:34 PM is there any function can : round(3.6) //result 4 round(3.4) //result 3 ? |

DOor

03-11-2009, 03:59 PM

I know I have had this issue before but for the life of me cant remember if I found a command to achieve this.

off the top of my head I know you could get the decimal by using % and then if the decimal is greater than .5 you could Ceil the number, if its less than .5 you could Floor it.

the below code will give you the results you need. note the % 1 that i did-- that is dividing by 1 and getting the remainder (so you get only the decimal of the float) then I just check if it is above or below 0.5.

float $myNumber = 3.2;

float $n = $myNumber % 1;

if ($n > 0.5) {

$myNumber = `ceil $myNumber`;

} else {

$myNumber = `floor $myNumber`;

}

print $myNumber;

off the top of my head I know you could get the decimal by using % and then if the decimal is greater than .5 you could Ceil the number, if its less than .5 you could Floor it.

the below code will give you the results you need. note the % 1 that i did-- that is dividing by 1 and getting the remainder (so you get only the decimal of the float) then I just check if it is above or below 0.5.

float $myNumber = 3.2;

float $n = $myNumber % 1;

if ($n > 0.5) {

$myNumber = `ceil $myNumber`;

} else {

$myNumber = `floor $myNumber`;

}

print $myNumber;

YourDaftPunk

03-11-2009, 11:33 PM

Try adding 0.5 and calling floor. Drop it in a function callled round() and you are set:

global proc float round(float $x) {

return floor($x + 0.5);

}

// some float number

float $random = rand(0,10);

// round to int

$rounded = round($random);

global proc float round(float $x) {

return floor($x + 0.5);

}

// some float number

float $random = rand(0,10);

// round to int

$rounded = round($random);

DOor

03-11-2009, 11:42 PM

ah nice, the +.5 method is definitely more efficient. I knew there was a better way, couldnt think of anything off the top of my head. :shrug:

dbiggs

03-12-2009, 03:17 PM

Here's a more complex version for rounding to n decimal places.

//

// Roundoff a float to $n places.

//

global proc float roundoff( float $f, int $n )

{

// we divide if n < 0 to avoid numeric

// precision problems

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 );

}

}

//

// Roundoff a float to $n places.

//

global proc float roundoff( float $f, int $n )

{

// we divide if n < 0 to avoid numeric

// precision problems

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 );

}

}

berniebernie

03-12-2009, 06:57 PM

i think there's a trick with INTs: http://www.foreachloop.com/blog/?p=7

or

http://forums.cgsociety.org/showthread.php?t=137969&highlight=roundoff

it's a little silly that it's not included as a standard function

edit: did not read post above mine =)

or

http://forums.cgsociety.org/showthread.php?t=137969&highlight=roundoff

it's a little silly that it's not included as a standard function

edit: did not read post above mine =)

acidream

03-13-2009, 12:49 AM

heres how i round numbers:

(floor(number_to_round * 10) / 10)

if the number is 44.53256222 it will return 44.5

change the 10 to 100 and it will return 44.53

(floor(number_to_round * 10) / 10)

if the number is 44.53256222 it will return 44.5

change the 10 to 100 and it will return 44.53

CGTalk Moderation

03-13-2009, 12:49 AM

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.

vBulletin v3.0.5, Copyright ©2000-2015, Jelsoft Enterprises Ltd.