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. 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
|
Original Thread
11-22-2005 13:12
_____________________
i've got nothing. 
|
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
|
11-22-2005 13:54
It should be noted that this function will fail on numbers larger then (2 ^ 31) - 1 and on numbers less then zero. The function is a good start but doesn't meet expectations.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river. - Cyril Connolly
Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence. - James Nachtwey
|
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
|
11-22-2005 14:47
From: Strife Onizuka It should be noted that this function will fail on numbers larger then (2 ^ 31) - 1 and on numbers less then zero. The function is a good start but doesn't meet expectations. If you have a way to round numbers or even truncate them that works on numbers over 2^31-1 I'd like to add that code to my altimeter. The only solutions I can think of involve string operations, or limiting truncation to a range.
|
Ben Bacon
Registered User
Join date: 14 Jul 2005
Posts: 809
|
11-23-2005 00:20
From: Argent Stonecutter If you have a way to round numbers or even truncate them that works on numbers over 2^31-1 I'd like to add that code to my altimeter. Well, the OP's code emits a string - if you're happy with this you could just cast the float directly to a string, and then see Strife's code below before edit: ...and then llSubStringIndex the decimal point. Truncate the string, x characters after the point, rather than truncating the number.
|
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
|
11-23-2005 00:34
string trunc(float a, integer offset) { string b = (string)a;//floats always have the "." at -7 llDeleteSubString(b, -7 + offset + (offset != 0),-1); }
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river. - Cyril Connolly
Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence. - James Nachtwey
|
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
|
11-23-2005 07:21
I hadn't noticed that it was always 7 digits after the decimal point.
That makes things simpler. Thanks.
|