?
Solved

Calculating a percentage of a range of numbers.

Posted on 2011-04-30
12
Medium Priority
?
501 Views
Last Modified: 2012-05-11
Hello,

Very sorry - rubbish title I know, can't say I get much better at them either.

I've got two, a minimum and a maximum now all I know is that the minimum will always be less than or equal to the maximum, there's no restrictions of having negative numbers etc (which is where I run into problems).

Both of those numbers are inclusive, so if I had:

min: 0
max: 10

The available number of numbers is:

result = (max-min)+1 =
result = (10-0)+1 =
result = 11

What I then want to do is find a particular percentage of that range, so taking the result above (11), and I wanted 30% of this I could easily do:

result = result * 0.3
result = 3

This is all okay, but as soon as negative numbers for minimum and/or maximum are thrown in, then things start to collapse quite quickly. Consider:

max = 5
min = -5
result = (max-min)+1 =
result = (5-(-5))+1 =
result = 11

result = result * 0.3
result = 11 * 0.3
result = 3
What I'm struggling with is that while 3 is correct for a range of 0-11, the actual range is -5 to 5. Yet I can't just subtract the minimum (-5) from the result 3 as that just gives me 8.

So, is there a way given an inclusive range (min to max) to get the percentage along that number line, perhaps without all the messy calculations above?

Thanks,
Uni
0
Comment
Question by:Unimatrix_001
  • 7
  • 4
12 Comments
 
LVL 2

Expert Comment

by:robertkennedy
ID: 35497693
Should you subtract from the minimum value or add to the minimum value?
Does this vary depeneding if the min value is a negative?

Maybe you just need to add some logic to check if the minimum value is less than 0 and change the operator from a subtraction to an addition
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 35497701
Should you subtract from the minimum value or add to the minimum value?
Does this vary depeneding if the min value is a negative?
Don't have a clue...

Maybe you just need to add some logic to check if the minimum value is less than 0 and change the operator from a subtraction to an addition
That is one solution, but it seems I'm over complicating something which seems very simple...
0
 
LVL 2

Expert Comment

by:robertkennedy
ID: 35497735
It seems you want to get a the value of the integer that is a percentage along the number line, correct?

So in the first instance this would be 0+3=3
And in the second I instance this would be -5+3=-2

Is this what you are after? If so you can always use an addition and this will return what you require. You could also subtract the inverse of the percentage from the maximum which would give the same result.

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Author Comment

by:Unimatrix_001
ID: 35497749
It seems you want to get a the value of the integer that is a percentage along the number line, correct?

Yes. Here's another example (Sorry I didn't quite grasp yours):

min: 10
max: 25
range: (25-10)+1 : 16

35% of 16 is: 5.6
min is 10, so actual result is: 5.6+10 = 15.6 ~~ 15

0
 
LVL 2

Assisted Solution

by:robertkennedy
robertkennedy earned 1000 total points
ID: 35497795
I think the calculations you use are necessary and I don't think the negative numbers will pose an issue, below is a summary of calculations.

Given:
min
max

AvailableNumbersInRange = (max - min) + 1

30 percent is therefore:

Place  = 0.3 * AvailableNumbersInRange

Value = min + Place
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 35497829
Hi Robert,

I'm getting some off by one error here, and I'm not sure why:

min: 0
max: 99
AvailableNumbersInRange = (99-0)+1
AvailableNumbersInRange = 100

If I want 100%:

Place = 1 * AvailableNumbersInRange
Place = 100
Value = 0 + 100
Value = 100

Value should not be greater than max...

Thanks,
Uni
0
 
LVL 2

Expert Comment

by:robertkennedy
ID: 35497866
In that case you would be best to use the inverse... (place from max)

place = (1-x%) * AvailableNumbersInRange
Value = max - place

So for your example above:

Place = (1 - 1)  * 100
Place = 0

Value = 99 - 0
Value = 99

0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 35497878
I'm still getting an off by one error, this time when the %==0:

min: 0
max: 99
AvailableNumbersInRange = (99-0)+1
AvailableNumbersInRange = 100

If I want 0%:

Place = (1-%) * AvailableNumbersInRange
Place = (1-0) * 100
Place = 1 * 100

Value = max - place
Value = 99 - 100
Value = -1
0
 
LVL 13

Accepted Solution

by:
Superdave earned 1000 total points
ID: 35498638
I don't think you should be adding 1 in the first place.  Unless you specifically want the number of integers in the range, in which case 100 is correct.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 35498684
Hi superdave,

>>I don't think you should be adding 1 in the first place.
If I don't add 1 then the top value cannot be selected.

>>in which case 100 is correct.
No, the final value must be >= min and <= max, otherwise min and max are useless.

Thanks,
Uni
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 35498807
Sorry superdave, I've just realised which +1 you are talking about - that seems to help...

Thanks,
Uni
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 35498811
:)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Complex Numbers are funny things.  Many people have a basic understanding of them, some a more advanced.  The confusion usually arises when that pesky i (or j for Electrical Engineers) appears and understanding the meaning of a square root of a nega…
This article seeks to propel the full implementation of geothermal power plants in Mexico as a renewable energy source.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses
Course of the Month16 days, 22 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question