Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Reducing number of extraneous decimals?

Hg Beeks
llGetElement(80);
Join date: 13 Apr 2006
Posts: 134
06-10-2007 01:48
I've got a script I've been working on that is essentially meant to make duplicating the properties of an object I've made that much easier; a simple script that basically spits out a fully-set up list for a llSetPrimitiveParams command. However, I'm finding that llGetPrimitiveParams tends to return a HUGE number of extra decimals that I can't really use; I only need the first three, max. The result is a total of four lines more code - Double the lines. Is there a way to make it quickly remove those excess decimals (All 0s at this point)?
Lazink Maeterlinck
Registered User
Join date: 8 Nov 2005
Posts: 332
06-10-2007 02:36
Use the string functions llSubStringIndex and llGetSubString, look for the decimal point then add 3 to that indext and llGetSubString with the +3 number.

CODE


float somenumber = 123.32324123;
integer index = llSubStringIndex((string) somenumber, ".");
string trunkated = llGetSubString((string) somenumber, 0, index + 3);


Boss Spectre
Registered User
Join date: 5 Sep 2005
Posts: 229
06-10-2007 03:20
"The fault, dear Brutus, lies not in our floats but in our strings..."

There's no built-in way to format floats being cast to strings as less than 6 decimal places, are you looking for a function to do this to the resulting strings? And do you need the result to be rounded based on the truncated digits?
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
06-10-2007 06:40
Even easier... since a float cast to a string always contains 6 digits after the decimal, simply truncate the last 3 characters:

float F = 12.34567890;
string S = llGetSubString( (string)F, 0, -4 ); // "12.345"
Ged Larsen
thwarted by quaternions
Join date: 4 Dec 2006
Posts: 294
06-10-2007 06:50
It's not what the poster asked for, but here's a quick and dirty function I use to return 'nicer' looking floats cast to strings:

CODE

string returnFloat(float fFloat)
//
// function to return a FLOAT type cast to STRING, by Ged Larsen
// - truncates all trailing 0's
// - or, if all zeros after the decimal, returns the integer component and ".0"
//
{
string sNumber = (string)fFloat;
integer len = llStringLength(sNumber);
while ( llGetSubString(sNumber, len - 1, len - 1) == "0" )
{
len--;
}
if (llGetSubString(sNumber, len - 1, len - 1) == ".")
{
return llGetSubString(sNumber, 0, len - 1)+"0"; // return "1.0" instead of "1."
}
else
{
return llGetSubString(sNumber, 0, len - 1); // return all except traililng 0's
}
}
_____________________
- LoopRez, flexi prim skirt generating tool
- LinkRez, a necklace chain generator
Hg Beeks
llGetElement(80);
Join date: 13 Apr 2006
Posts: 134
06-10-2007 11:40
I don't have much choice but to use strings; llGetPrimitiveParams gives a multitude of information types (Keys, vectors, floats, and integers) and some of those are not castable to vectors.
While the methods mentioned here would work if all the information in the list were simple vectors, it would result in chopping off parts of keys, and making vectors unusable if I just did it straight as suggested.
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
06-10-2007 16:00
Hg, what are you doing with the data retrieved from llGetPrimitiveParams? Storing it in script memory? Dumping it to chat/IM?
Hg Beeks
llGetElement(80);
Join date: 13 Apr 2006
Posts: 134
06-10-2007 20:37
Storing it in the script just long enough to format an llOwnerSay.
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
06-10-2007 22:08
So you're ultimately dumping it to chat. What's wrong with saving the text as it's formatted by, say, llList2CSV?
Hg Beeks
llGetElement(80);
Join date: 13 Apr 2006
Posts: 134
06-11-2007 03:51
While that's a very handy command that I wasn't previously aware of, there are a couple of reasons;
A) PRIM_TEXTURE spits back a key, and to properly format it, I have to add the quotations around it, and
B) That doesn't solve the problem; In fact, doing that would make it MORE difficult to remove extra decimal places.
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
06-11-2007 05:27
From: Hg Beeks
PRIM_TEXTURE spits back a key, and to properly format it, I have to add the quotations around it
Only if you're pasting it directly into a script. Is that what you're doing with the data, once it's been dumped to llOwnerSay?
Learjeff Innis
musician & coder
Join date: 27 Nov 2006
Posts: 817
06-11-2007 09:34
From: Deanna Trollop
Even easier... since a float cast to a string always contains 6 digits after the decimal, simply truncate the last 3 characters:

float F = 12.34567890;
string S = llGetSubString( (string)F, 0, -4 ); // "12.345"


minor correction:

string S = llGetSubString((string)(F + 0.0005), 0 -4); // "12.346"
Hg Beeks
llGetElement(80);
Join date: 13 Apr 2006
Posts: 134
06-11-2007 12:09
From: Deanna Trollop
Only if you're pasting it directly into a script. Is that what you're doing with the data, once it's been dumped to llOwnerSay?

Yes. I'm beginning to think the best way to deal with this is would be to simply live with the extra decimals and use the script editor to replace 000.