Luke Rowley Posted July 7, 2017 Share Posted July 7, 2017 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 More sharing options...
Rachel1206 Posted July 7, 2017 Share Posted July 7, 2017 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); } 1 Link to comment Share on other sites More sharing options...
Luke Rowley Posted July 7, 2017 Author Share Posted July 7, 2017 Thank you for your answer, it works well. I'm just a bit sad that I need this function just to display a correct float. Link to comment Share on other sites More sharing options...
Love Zhaoying Posted July 7, 2017 Share Posted July 7, 2017 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 More sharing options...
Luke Rowley Posted July 7, 2017 Author Share Posted July 7, 2017 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 More sharing options...
Love Zhaoying Posted July 7, 2017 Share Posted July 7, 2017 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 More sharing options...
Luke Rowley Posted July 7, 2017 Author Share Posted July 7, 2017 No problem at all. Link to comment Share on other sites More sharing options...
Nova Convair Posted July 7, 2017 Share Posted July 7, 2017 @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. 2 Link to comment Share on other sites More sharing options...
Rolig Loon Posted July 7, 2017 Share Posted July 7, 2017 (edited) 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 July 7, 2017 by Rolig Loon 1 Link to comment Share on other sites More sharing options...
Love Zhaoying Posted July 7, 2017 Share Posted July 7, 2017 D'oh! I see now, of COURSE you have to strip the extra digits with string functions. Because LSL. Link to comment Share on other sites More sharing options...
Love Zhaoying Posted July 7, 2017 Share Posted July 7, 2017 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 More sharing options...
Nova Convair Posted July 7, 2017 Share Posted July 7, 2017 (string)(NewNum/100)) + "." + (string)(NewNum%100) will turn 21.04 into 21.4 It's not easy to make something simple. Link to comment Share on other sites More sharing options...
Rolig Loon Posted July 7, 2017 Share Posted July 7, 2017 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 More sharing options...
Nova Convair Posted July 8, 2017 Share Posted July 8, 2017 I did that all myself. Write something simple - test it - make a stupid face - and when it finally handles all situations and special cases it's not that simple anymore. Link to comment Share on other sites More sharing options...
Luke Rowley Posted July 8, 2017 Author Share Posted July 8, 2017 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. Link to comment Share on other sites More sharing options...
Rolig Loon Posted July 8, 2017 Share Posted July 8, 2017 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. 2 Link to comment Share on other sites More sharing options...
Dora Gustafson Posted July 8, 2017 Share Posted July 8, 2017 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 1 Link to comment Share on other sites More sharing options...
Rachel1206 Posted July 8, 2017 Share Posted July 8, 2017 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 - so my suggestion above will work with intercontinental missiles and would be approved by The National Bank of SL , I hope Link to comment Share on other sites More sharing options...
Rolig Loon Posted July 8, 2017 Share Posted July 8, 2017 1 hour ago, Rachel1206 said: my suggestion above will work with intercontinental missiles There's a crazy guy in North Korea who might be able to use it, Rachel. I'm sure the NSA wouldn't mind. 1 Link to comment Share on other sites More sharing options...
Berksey Posted July 8, 2017 Share Posted July 8, 2017 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 More sharing options...
Wulfie Reanimator Posted July 13, 2017 Share Posted July 13, 2017 (edited) 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 July 13, 2017 by Wulfie Reanimator 1 Link to comment Share on other sites More sharing options...
Love Zhaoying Posted July 17, 2017 Share Posted July 17, 2017 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.) 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Please take a moment to consider if this thread is worth bumping.
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now