Jump to content

Typecast float


Luke Rowley
 Share

You are about to reply to a thread that has been inactive for 1526 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hi !

 

I run into a problem where I have some issue to correctly display some float values, this is my function:

llSetLinkPrimitiveParamsFast(1, [PRIM_TEXT, "FC\n" + (string)((float)value / precisionFloat), <1, 1, 1>, 1]);

 

Value is a string from a JSON object. Its value change from 10000000 to an higher number.

precisionFloat is an amount of numbers I want after the , (It's not implement yet.)

 

However, I get result like this: https://i.imgur.com/4EuiwqF.gifv

Is there a way to prevent this ? The fact that it's not round number even tho numbers used are.

 

Thank you. :)

Link to comment
Share on other sites

Yes we miss some basic LSL functions as for example "%.2f".

I use as below to display rounded float values:

string FormatDecimal(float fNumber, integer nPrecision)
{    
    float fRounded = llPow(10, -nPrecision)*0.5;
    float fResult;
    if (fNumber < 0)
        fResult = fNumber - fRounded;
    else            
        fResult = fNumber + fRounded;
 
    if (nPrecision < 1)
    {
        integer nTmp = (integer)llPow(10, -nPrecision);
        fResult = (integer)fResult/nTmp*nTmp;
        nPrecision = -1;
    }
 
    string strNumber = (string)fResult;
    return llGetSubString(strNumber, 0, llSubStringIndex(strNumber, ".") + nPrecision);
}

 

  • Like 1
Link to comment
Share on other sites

Gee, if you only need 2 digits past decimal, why not do something simple like this:

Float Convert2Decimal(float Original) {

  integer IntValue;

  float FloatValue;

  IntValue=(integer)(Original*100);  // will preserve 2 past decimal

  FloatValue=((float)IntValue) / 100.0;  // Put the 2 decimal places back

  return FloatValue;

}

Maybe I'm dumb, but I like simple things.

Link to comment
Share on other sites

15 minutes ago, Love Zhaoying said:

Maybe I'm dumb, but I like simple things.

I do love simply things too, but sadly your solution and the solutions I've been working on since yesterday don't work.

For now, only @Rachel1206's answer works. If I ever found another solution I will post it here

Link to comment
Share on other sites

Just now, Heyter Nitely said:

I do love simply things too, but sadly your solution and the solutions I've been working on since yesterday don't work.

For now, only @Rachel1206's answer works. If I ever found another solution I will post it here

Sorry, if I was not at work I could test different simple solutions in SL.

Link to comment
Share on other sites

@Love - your script works. It uses the exact same math as Rachel's - but the bells and whistles are missing and that's what the op needed.

- you cut the result and rachel rounds it.
- you have a fixed precision and rachels is variable
- you output a float and rachel outputs a formatted string - that's what the op needed.

But the math formula is the same. Though -  without rounding it will return 3.9968 as 3.99 and with rounding it returns a 4 which is better in most cases.

  • Like 2
Link to comment
Share on other sites

As far as I know, these forums won't let me post a link to http://www.sluniverse.com/php/vb/scripting/125191-help-me-tidy-up-my.html  , but I'll try.  If not, take a look in the Scripting forum in SLUniverse for a recent thread with the title " Help me tidy up my decimal places ? ".  There was a good discussion with several clever solutions to your problem.

EDIT:   Oooo!   Maybe it worked.

Edited by Rolig Loon
  • Like 1
Link to comment
Share on other sites

5 minutes ago, Rolig Loon said:

As far as I know, these forums won't let me post a link to http://www.sluniverse.com/php/vb/scripting/125191-help-me-tidy-up-my.html  , but I'll try.  If not, take a look in the Scripting forum in SLUniverse for a recent thread with the title " Help me tidy up my decimal places ? ".  There was a good discussion with several clever solutions to your problem.

EDIT:   Oooo!   Maybe it worked.

I like your first solution at the link:

string Truncate (float num)
{
     integer NewNum = (integer)(num *100.0);
     return ((string)(NewNum/100)) + "." + (string)(NewNum%100));
}
Link to comment
Share on other sites

8 minutes ago, Nova Convair said:

(string)(NewNum/100)) + "." + (string)(NewNum%100)

will turn 21.04 into 21.4

It's not easy to make something simple.

I know.  :$  Void pointed that out in her comment following my post and offered an improved version that I like much better.   That's what happens when we write scripts on the fly and they don't get a full test before they land in a forum. 

Link to comment
Share on other sites

Sad?  Not really.  IMO, one of the joys of scripting is discovering that some of the "simple" tasks that we take for granted in life are much more complicated than they look.  Our challenge is to find the easiest way to encode them. I love finding little puzzles like this.  B|

  • Like 2
Link to comment
Share on other sites

This has not been suggested yet:

string truncate_float_decimals( float x, integer n)
{
    string s = (string)x;
    integer i = llSubStringIndex( s, ".");
    return llGetSubString( s, 0, i-1)+llGetSubString( s, i, i+n);
}

It is straight forward
In this form it doesn't work with scientific notation and it does not round but it is simple and can easily be modified for special needs

This code is not tested but the principle is

 

  • Like 1
Link to comment
Share on other sites

I learned the need for precision many years ago as an young developer, where I had to handle milliseconds correct to be displayed within a scientific program B| - so my suggestion above will work with intercontinental missiles and would be approved by The National Bank of SL :D, I hope -_-

Link to comment
Share on other sites

What Rolig said above a few posts up rings so true. This stuff would be way more boring if it didn't throw up an obstacle here and there that had to be surmounted.

Honestly, typing up a script and having it work right off isn't nearly as satisfying as typing up a script, having it do something funky or incorrect, and figuring out why. I've learned more from working around obstacles, bugs and limitations than I have from any other aspect of scripting (or anything else, really).

Link to comment
Share on other sites

On 7/8/2017 at 4:10 PM, Heyter Nitely said:

Thanks guys for your multiple answers.

i'm currently using @Rachel1206's function and it works like a charm so far. That's kinda sad that we must use a function just to display a right float. :(  

If you absolutely don't want to deal with a user-defined function, you could easily cram everything on a single line, or if you only ever need the rounding in one place, handle the process there instead. (At least I assume you meant user-defined functions, because you have to use regular functions to display anything at all anyway.)

llSetLinkPrimitiveParamsFast(1, [PRIM_TEXT,
        "FC\n" + llGetSubString((string)value, 0, 2+llSubStringIndex((string)value, ".")),
        <1, 1, 1>, 1]);

Just change the "2" into anything between 1 and 6. (Though this is truncation only. But likewise Rachel's function makes small rounding errors.)
And I know I'm roughly 3 days late, but I'm tired and wanted to pass the time before bed.

Edited by Wulfie Reanimator
  • Like 1
Link to comment
Share on other sites

On 7/8/2017 at 3:41 PM, Rachel1206 said:

my suggestion above will work with intercontinental missiles

It would be more useful if it worked with intercontinental BALLISTIC missiles. That way you get icey BM's, as opposed to just ICM's.  And who doesn't want to drop a frozen turd on someone from an airplane? (That's an icey BM.)

  • Like 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1526 days.

Please take a moment to consider if this thread is worth bumping.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...