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;
}
THANKS!