x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

AnswerHub Maintenance

Background maintenance is scheduled to occur between 9 - 11am EDT on Tuesday, May 21. Site operation may be slower than normal during this time and a brief interruption in operation may be observed

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

Product Version: UE 4.14
Tags:
more ▼

asked May 19 '17 at 07:40 PM in Blueprint Scripting

avatar image

Freudian
1 1 2 6

(comments are locked)
10|2000 characters needed characters left

4 answers: sort voted first

Multiply by a fraction.

    1. = 100%

    1. = 70%

    1. = 30%

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

more ▼

answered May 19 '17 at 07:51 PM

avatar image

Jamie Dale STAFF
7.4k 176 66 304

avatar image Freudian May 03 '18 at 07:46 PM

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

(comments are locked)
10|2000 characters needed characters left

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));
more ▼

answered May 19 '17 at 07:54 PM

avatar image

janimationd
424 20 95 122

(comments are locked)
10|2000 characters needed characters left

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.alt text

screenshot-15.png (38.1 kB)
more ▼

answered Sep 02 '18 at 02:58 PM

avatar image

mattyi85
16 1 4 8

(comments are locked)
10|2000 characters needed characters left

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).

more ▼

answered Feb 21 '19 at 07:35 PM

avatar image

Frednotbob
1 1 2

avatar image Jamie Dale STAFF Feb 21 '19 at 07:45 PM

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.

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question