logo
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Truncating float returns (Read 1693 times)
gopostal
Betatester
Offline


Hack coder

Posts: 943
Joined: Jul 31st, 2008
Gender: Male
Truncating float returns
Sep 8th, 2019 at 4:31pm
Print Post  
UEngine defaults float returns to six decimals. For example you would get the return of "3.333333" when the decimal repeats. Is there a simple way to truncate that return to one or two decimal places without having to convert to a string value and then parsing the string after the "."? I looked through the function list and I can't find anything.
  

I don't want to give the end away
but we're all going to die one day
Back to top
 
IP Logged
 
yrex .
Betatester
Offline


Oldunreal member

Posts: 227
Joined: May 6th, 2015
Re: Truncating float returns
Reply #1 - Sep 9th, 2019 at 3:01pm
Print Post  
int(x*100)/100, but it may introduce some error.
  

Quality content | mail me instead of PMing: ampoyrex(a)wp(.)pl
Back to top
 
IP Logged
 
gopostal
Betatester
Offline


Hack coder

Posts: 943
Joined: Jul 31st, 2008
Gender: Male
Re: Truncating float returns
Reply #2 - Sep 11th, 2019 at 1:08pm
Print Post  
Thank you y, this is exactly what I needed. I see the introduced error but it's slight enough that it works just fine.

BTW, in case this bubbles up in a google search here is how I was able to get to where I wanted to go...
Code
Select All
//Kills to Deaths Ratio computation (protects from divide-by-zero)
If(PRI.Deaths <= 0)
    KDR = (PRI.Score);
Else If(PRI.Score <= 0)
    KDR = (PRI.Score);
Else KDR = (PRI.Score / PRI.Deaths);

//Color the KDR if it's bad
If (KDR < 1.0)
   Canvas.DrawColor = RedColor;
   //Truncate the value to two decimals
   if(KDR != 0)
   {
        K = int(KDR *100);
        TruncatedKDR = K /100;
	G = sf(TruncatedKDR);
   }

   //Draw the KDR
   Canvas.DrawText(G, False); 


It won't draw anything until there are kills or deaths. I might change that to something else in the final product but for now this shows how to get here.

...and here's the function to strip the trailing zeros
Code
Select All
// Cuts off trailing 0's.
function string sf(float f)
{
    local string s;

    s = string(f);
    while (right(s, 1) == "0")
        s = left(s, len(s) - 1);

    if (right(s, 1) == ".")
        s = s $ "0";

    return s;
} 


I found this function in a class called "Utils" in the RocketX mods. For proper credits here is the header from the class
Quote:
// VMRX6.
//      ""(§)FrÅgÊd(§)""
//               **Monarch**
//
//              555 ---===CrAzYBoNeS===--- 555
//                  Boppolis_The_Dog
« Last Edit: Sep 11th, 2019 at 4:54pm by gopostal »  

I don't want to give the end away
but we're all going to die one day
Back to top
 
IP Logged
 
[]KAOS[]Casey
Developer Team
Betatester
Offline


nedm

Posts: 3214
Joined: Aug 7th, 2011
Gender: Male
Re: Truncating float returns
Reply #3 - Sep 12th, 2019 at 4:12am
Print Post  
it's too bad fstring printf was never exposed to unrealscript, i use it all the time in native code
  
Back to top
 
IP Logged
 
gopostal
Betatester
Offline


Hack coder

Posts: 943
Joined: Jul 31st, 2008
Gender: Male
Re: Truncating float returns
Reply #4 - Sep 12th, 2019 at 1:59pm
Print Post  
Your posts came up in almost every google search I did trying to solve this. I admit I didn't understand what you were advocating for but now that I've dealt with the issue I see what you mean. There needs to be a better way to get from A to B on this, something native and much faster than code to truncate returns.
  

I don't want to give the end away
but we're all going to die one day
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint
Bookmarks: del.icio.us Digg Facebook Google Google+ Linked in reddit StumbleUpon Twitter Yahoo