For direct access use https://forums.oldunreal.com
It's been quite a while since oldunreal had an overhaul, but we are moving to another server which require some updates and changes. The biggest change is the migration of our old reliable YaBB forum to phpBB. This system expects you to login with your username and old password known from YaBB.
If you experience any problems there is also the usual "password forgotten" function. Don't forget to clear your browser cache!
If you have any further concerns feel free to contact me: Smirftsch@oldunreal.com

Conversion from float to string using the shortest notation

The section related to UnrealScript and modding. This board is for coders to discuss and exchange experiences or ask questions.
Post Reply
User avatar
Masterkent
OldUnreal Member
Posts: 1469
Joined: Fri Apr 05, 2013 12:41 pm

Conversion from float to string using the shortest notation

Post by Masterkent »

The standard float-to-string conversion works like sprintf(s, "%f", arg) and produces a string containing 6 digits after the point sign. For example, the value of floating point literal 32.1 is converted into "32.099998". In some cases I'd rather prefer more user-friendly conversions like 32.1 -> "32.1".

Is there any standard UScript function that performs conversion like sprintf(s, "%g", arg)?
User avatar
[]KAOS[]Casey
OldUnreal Member
Posts: 4497
Joined: Sun Aug 07, 2011 4:22 am
Location: over there

Re: Conversion from float to string using the shortest notation

Post by []KAOS[]Casey »

I'm afraid you'd have to write your own. I don't think anyone has ever done this, aside from arbitrarily cutting off digits with left(s,n)

That said, if it were me I'd write a wrapper for (s)printf in c++
User avatar
Masterkent
OldUnreal Member
Posts: 1469
Joined: Fri Apr 05, 2013 12:41 pm

Re: Conversion from float to string using the shortest notation

Post by Masterkent »

I'm afraid you'd have to write your own.
I thought about using such a function for parameters that can be changed via game menus (FOV, dodge click time, respawn time for flares and seeds). An implementation of proper float-to-string conversion in UScript would be complicated and I can't seriously consider such an approach knowing that a core function might implement the same thing using much less efforts.
That said, if it were me I'd write a wrapper for (s)printf in c++
I presume, FString::Printf("%g", floating_point_value) would do the right thing. There is only one person who can add the corresponding UScript core function for it.
Last edited by Masterkent on Thu Dec 01, 2016 6:48 pm, edited 1 time in total.
User avatar
[]KAOS[]Casey
OldUnreal Member
Posts: 4497
Joined: Sun Aug 07, 2011 4:22 am
Location: over there

Re: Conversion from float to string using the shortest notation

Post by []KAOS[]Casey »

Might as well go the whole 9 yards and just have FString::Printf accessible through uscript, using TArray as varargs
User avatar
han
Global Moderator
Posts: 686
Joined: Wed Dec 10, 2014 12:38 am

Re: Conversion from float to string using the shortest notation

Post by han »

I don't think using arrays for specifying varargs would be that great idea, as this get very cumbersome to use.

DeusEx has som printf uc version which uses optional parameters instead of varrags, which would limit the number of arguments, but it gets a lot easier to use. It doesnt handle float specifiers or anything though, but might be a start to implement them. E.g. split the %f string parameters at ., convert them to int and process further. However, this won't ensure correct rounding. E.g. 0.0000045 would get rounded to 0.000005 on string conversation, and for lets say 5 digits after the dot it would end up as 0.00001, while the correct rounded version would be 0.00000.

Code: Select all


//
// DEUS_EX CNN - Sprintf function for simple string formatting
// mostly useful for localized strings
//
// Format specifiers are - %s for string and %d for integer
// (Though, really, %any_single_character will work)
// Use %% to actually print the percent sign
//

final function string Sprintf      (string fmt,
                                                optional coerce string s0,
                                                optional coerce string s1,
                                                optional coerce string s2,
                                                optional coerce string s3)
{
      local string str, s[4];
      local int pos, index;

      // init the parameters since we can't pass in parms in arrays
      s[0] = s0;
      s[1] = s1;
      s[2] = s2;
      s[3] = s3;

      // init the counters
      index = 0;
      pos = 0;
      str = "";

      // parse the string
      do
      {
            pos = InStr(fmt, "%");

            // end of string?
            if (pos == -1)
            {
                  str = str $ fmt;
                  fmt = "";
            }
            else      // else, parse the parameter
            {
                  str = str $ Left(fmt, pos);

                  // check for %% special case
                  if ((pos < Len(fmt)) && (Mid(fmt, pos+1, 1) == "%"))
                        str = str $ "%";
                  else
                        str = str $ s[index++];

                  // trim the format string including the parameter
                  fmt = Right(fmt, Len(fmt) - pos - 2);
            }
      }
      until ((pos == -1) || (index == 4));

      // if there's anything left
      if (fmt != "")
            str = str $ fmt;

      return str;
}
/edit:
For some time I have in my mind that some intBitsToFloat and floatBitsToInt might be useful in uc. At least they would be a way to enable one to write an own float to string conversation in uc, though my original intention for their usage was quite different.
Last edited by han on Mon Dec 05, 2016 9:06 am, edited 1 time in total.
HX on Mod DB. Revision on Steam. Löffels on Patreon.
User avatar
Masterkent
OldUnreal Member
Posts: 1469
Joined: Fri Apr 05, 2013 12:41 pm

Re: Conversion from float to string using the shortest notation

Post by Masterkent »

However, this won't ensure correct rounding.
A user menu should not use float-to-string conversions that do not guarantee the following equality:

FloatToString(float(FloatToString(f))) == FloatToString(f)

where f is any value of type float.
Post Reply

Return to “UScript Board”