Getting a percentage of a Float or Integer

Getting a percentage of a Float or Integer

I want to be able to set a Variable with a value that is equal to the percentage of another Integer or Float. For example, reducing the players movement speed at a percentage of 30%. The player, moving at a speed of 600 by default reduced by 30% (180) should give me 420 after calculating. I’m comfortable with the math I just cant for the life of me figure out how to calculate a percentage of an Integer. I basically just want to find a way of getting 30% of a Variable.

Thanks in advance
-F

Multiply by a fraction.

  • 1.0 = 100%
  • 0.7 = 70%
  • 0.3 = 30%

600 x 0.3 is 180, 600 x 0.7 is 420.

1 Like

In unreal engine, movement speed is a float, so the speed you are referring to should start off as a float before you do your calculations…

After that the calculations are simple:

float normalSpeed = 600;
float percentModifier = -30;
float newSpeed = normalSpeed * (1 + (percentModifier / 100));

Thanks and sorry for the long reply, I have been super busy it became pretty obvious when I sat down and had a second look at it that all’s I needed to do was treat 1 as 100% and just use decimal values ^_^’

That’s where 3 AM scripting gets you I guess haha

Cheers, Freudian

I see this is an old thread but thought i would add another way to work out percentages for anyone else that might be looking like i was the other day.

If you don’t want rounded numbers (so you can have 0.5 for example) then using floats is best. if you want rounded numbers (1,2,3 etc) then i still convert integers to float then back to integers using ceiling or floor functions depending which way you want to round up.

The MATH - for example you want to work out 10% of 62 (answer is 6.2) i take my number 62 then divide that by 100 (= 0.62) then multiply that by 10 = 6.2 (10% of 62).

For other percentages you just change the end multiplication so 20 % would be 62 divided by 100 multiplied by 20 = 12.4
multiply by 30 for 30% and so on.

1 Like

And another resurrection for those that look here in 2019:

As of 4.21.2, there is now an ‘as percent’ node available that will output the value of a float as ‘n%’. Logically, it’s under ‘text’, or you can right-click and search for ‘as percent’.

Keep in mind, though, that the integer will continue to increase past 100% unless you clamp the value of your float (not the min/max, but the value).

That node has existed since 4.0. It gives you a fractional number back as a culture correct percentage representation… but that wasn’t what the OP was asking for here, as they wanted a percentage of another number as a numeric value.

I realize this isn’t directly in the scope of what the OP was asking, but this is the top search result from Google, so I wanted to share how to compare a number as a percentage of another number and have this converted to a 0-1 range. This can be helpful if you’re trying to fill a progress bar for example.

Let’s imagine you have two numbers, a value representing the current progress (maybe your XP), and another representing the target (When you gain a level). You would divide the Current Progress by the Target to get its percentage of the target on a 0-1 scale.

CompareNumbersAsPercentage

This will work regardless of the values and is a great way to convert to a 0-1 range to fill a progress meter. If you want the percentage on a 0 - 100 scale, just multiply the result by 100.