View Full Version : Rounding off

04 April 2006, 10:45 PM
I'm wondering if there is a good way to round off float numbers so they only have three decimals.
Example: 0.6987459 = 0.699.

04 April 2006, 11:36 PM
Well I'm not sure if this can be done directly with a command specifically designed for such a task, but I guess that you can convert the float to a string and then chop the remaining characters you don't want from the end using a combination of the endString and basename commands.

04 April 2006, 06:57 AM
Search this forum, I once posted a procedure written by incredible Duncan Brimsmead that rounds off floats to the decimal you specify. I don`t have it here, so I can`t share it. But searching should give you a result.

found it here

05 May 2006, 07:40 PM
maybe a little late to the party, but I remembered the original post and did a search for it to see if there were any solutions, since I needed a function to round off a number. Didn't see any, so I made my own. Here you go:

global proc float dbRoundOff(float $val, int $accuracy)
string $tempVal = $val;
float $returnVal;

int $intVal = $val;

$tempVal = `match "[^\.]*$" $tempVal`;

string $roundString = `substring $tempVal ($accuracy + 1) ($accuracy + 1)`;
int $roundInt = $roundString;

if($accuracy < 1) {
if($roundInt >= 5)

$returnVal = $intVal;
return $returnVal;

$tempVal = startString($tempVal, $accuracy);

int $decimalValue = $tempVal;
if($roundInt >= 5)

$tempVal = $intVal + "." + $decimalValue;
$returnVal = $tempVal;

return $returnVal;

05 May 2006, 10:48 PM
to truncate down to the nearest accuracy step:$val = $accuracy*trunc($val/$accuracy);to round, add one half in the trunc:$val = $accuracy*trunc($val/$accuracy+0.5);

CGTalk Moderation
05 May 2006, 10:48 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.