Correct way to remove trailing zeroes from sim stats

Creating scripts
Post Reply
User avatar
Lunk Portal
Posts: 150
Joined: Wed Jan 26, 2022 10:03 am
Location: Northern Michigan
Has thanked: 100 times
Been thanked: 151 times
Contact:

Correct way to remove trailing zeroes from sim stats

Post by Lunk Portal »

Greetings everyone.

I have been working on a script for proper sim stats. For the life of me, I cannot seem to figure out how to properly remove the trailing 000000s from the end of each result.

Anyone know a simple script to do this?

I tried the following with no luck;

Code: Select all

    // Fetch memory stats (Example for index 44 and 45)
    float memory1 = llList2Float(stats, 44);
    float memory2 = llList2Float(stats, 45);

    // Format memory values to remove trailing zeros
    string formattedMemory1 = format_float(memory1, 2); // Round to 2 decimal places
    string formattedMemory2 = format_float(memory2, 2); // Round to 2 decimal places
    
    // Function to round the float and remove trailing zeros
string format_float(float value, integer decimal_places) {
    // Multiply by 10^decimal_places, convert to integer, then divide by 10^decimal_places
    integer multiplier = (integer)(10.0 * decimal_places); // Ensure correct rounding
    float rounded_value = (float)((integer)(value * multiplier)) / multiplier;

    // Convert to string
    string result = (string)rounded_value;

    // Remove trailing zeros and the decimal point if it's unnecessary
    integer indexOfDecimal = llSubStringIndex(result, ".");
    if (indexOfDecimal != -1) {
        integer len = llStringLength(result);
        // Remove trailing zeroes
        while (len > 0 && llGetSubString(result, len - 1, len - 1) == "0") {
            result = llGetSubString(result, 0, len - 2);
            len--;
        }
        // If there's a decimal point left at the end, remove it
        if (llGetSubString(result, len - 1, len - 1) == ".") {
            result = llGetSubString(result, 0, len - 2);
        }
    }
    return result;
}
    
There has to be an easier way, with less code, and one that actually works...

THANKS!
These users thanked the author Lunk Portal for the post:
Ilan Tochner
User avatar
Tess Juel
Posts: 406
Joined: Sun Sep 11, 2016 4:24 pm
Has thanked: 324 times
Been thanked: 515 times

Re: Correct way to remove trailing zeroes from sim stats

Post by Tess Juel »

Here's one way that should work.
It's not really my code. It's based on a post by Kimpa Tammas posted on the Second Life forums a year or so ago. I only added a few tweaks. The most important is that I added a line to remove a potential redundant decimal point.

Code: Select all

string output=(string) inputvalue; //Assuming input is a float and you want to typecast it as a string. Ignore this if it's already a string.
while (llGetSubString(output, -1, -1) == "0") output = llDeleteSubString(output, -1, -1);
if (llGetSubString(output, -1, -1) == ".") output = llDeleteSubString(output, -1, -1);
---

Another alternative:

Code: Select all

string output=(string) inputvalue; //Assuming input is a float and you want to typecast it as a string. Ignore this if it's already a string.
if (llGetSubString(output, -7, -1) == ".000000") output = llDeleteSubString(output, -7, -1);
else if (llGetSubString(output, -5, -1) == "00000") output = llDeleteSubString(output, -5, -1);
else if (llGetSubString(output, -4, -1) == "0000") output = llDeleteSubString(output, -4, -1);
else if (llGetSubString(output, -3, -1) == "000") output = llDeleteSubString(output, -3, -1);
else if (llGetSubString(output, -2, -1) == "00") output = llDeleteSubString(output, -2, -1);
else if (llGetSubString(output, -1, -1) == "0") output = llDeleteSubString(output, -1, -1);
This works because a float typecast as a string will always have six decimals, no more and no less. It's a bit more code than the first solution but it should still execute faster since it removes all the superfluous characters in one go and then exits.
These users thanked the author Tess Juel for the post (total 2):
Ilan TochnerLunk Portal
User avatar
Lunk Portal
Posts: 150
Joined: Wed Jan 26, 2022 10:03 am
Location: Northern Michigan
Has thanked: 100 times
Been thanked: 151 times
Contact:

Re: Correct way to remove trailing zeroes from sim stats

Post by Lunk Portal »

Greatly appreciated! I will give it a go and see if I can get it to work. Thank you!

Tess Juel wrote:
Sat Apr 05, 2025 8:42 pm
Here's one way that should work.
It's not really my code. It's based on a post by Kimpa Tammas posted on the Second Life forums a year or so ago. I only added a few tweaks. The most important is that I added a line to remove a potential redundant decimal point.

Code: Select all

string output=(string) inputvalue; //Assuming input is a float and you want to typecast it as a string. Ignore this if it's already a string.
while (llGetSubString(output, -1, -1) == "0") output = llDeleteSubString(output, -1, -1);
if (llGetSubString(output, -1, -1) == ".") output = llDeleteSubString(output, -1, -1);
---

Another alternative:

Code: Select all

string output=(string) inputvalue; //Assuming input is a float and you want to typecast it as a string. Ignore this if it's already a string.
if (llGetSubString(output, -7, -1) == ".000000") output = llDeleteSubString(output, -7, -1);
else if (llGetSubString(output, -5, -1) == "00000") output = llDeleteSubString(output, -5, -1);
else if (llGetSubString(output, -4, -1) == "0000") output = llDeleteSubString(output, -4, -1);
else if (llGetSubString(output, -3, -1) == "000") output = llDeleteSubString(output, -3, -1);
else if (llGetSubString(output, -2, -1) == "00") output = llDeleteSubString(output, -2, -1);
else if (llGetSubString(output, -1, -1) == "0") output = llDeleteSubString(output, -1, -1);
This works because a float typecast as a string will always have six decimals, no more and no less. It's a bit more code than the first solution but it should still execute faster since it removes all the superfluous characters in one go and then exits.
User avatar
Lunk Portal
Posts: 150
Joined: Wed Jan 26, 2022 10:03 am
Location: Northern Michigan
Has thanked: 100 times
Been thanked: 151 times
Contact:

Re: Correct way to remove trailing zeroes from sim stats

Post by Lunk Portal »

THANKS!

The alternative worked perfectly!!
Image
Tess Juel wrote:
Sat Apr 05, 2025 8:42 pm
Here's one way that should work.
It's not really my code. It's based on a post by Kimpa Tammas posted on the Second Life forums a year or so ago. I only added a few tweaks. The most important is that I added a line to remove a potential redundant decimal point.

Another alternative:

Code: Select all

string output=(string) inputvalue; //Assuming input is a float and you want to typecast it as a string. Ignore this if it's already a string.
if (llGetSubString(output, -7, -1) == ".000000") output = llDeleteSubString(output, -7, -1);
else if (llGetSubString(output, -5, -1) == "00000") output = llDeleteSubString(output, -5, -1);
else if (llGetSubString(output, -4, -1) == "0000") output = llDeleteSubString(output, -4, -1);
else if (llGetSubString(output, -3, -1) == "000") output = llDeleteSubString(output, -3, -1);
else if (llGetSubString(output, -2, -1) == "00") output = llDeleteSubString(output, -2, -1);
else if (llGetSubString(output, -1, -1) == "0") output = llDeleteSubString(output, -1, -1);
This works because a float typecast as a string will always have six decimals, no more and no less. It's a bit more code than the first solution but it should still execute faster since it removes all the superfluous characters in one go and then exits.
Post Reply