Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Round to..

Haravikk Mistral
Registered User
Join date: 8 Oct 2005
Posts: 2,482
11-21-2005 12:18
I'm surprised that the LSL llRound() function doesn't already do this, but I've written a simple function for rounding a number to x decimal places.

CODE
string Round2String (float x, integer decPlaces) {
integer int = (integer) x;
integer remainder = llRound((x - int) * llPow(10,decPlaces));
if (!remainder) return (string)int;
return (string)int+"."+(string)remainder;
}


Simply feed it in the form of:

Round2String(3.9221312,2) = 3.92
Round2String(1.532, 1) = 1.5
Round2String(1.032932, 3) = 1.033

The answer returned is a string as I find that most useful, it can however be adapted to give out integers or floats quite happily.

Dunno if anyone's found a better way?
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Discussion Thread
11-22-2005 13:12
/54/21/73156/1.html
_____________________
i've got nothing. ;)