• Status: Solved
• Priority: Medium
• Security: Public
• Views: 508

# Calculating a percentage of a range of numbers.

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
Unimatrix_001
• 7
• 4
2 Solutions

Commented:
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

Author Commented:
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

Commented:
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

Author Commented:
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

Commented:
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

Author Commented:
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

Commented:
In that case you would be best to use the inverse... (place from max)

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

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

Value = 99 - 0
Value = 99

0

Author Commented:
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

Commented:
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

Author Commented:
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

Author Commented:
Sorry superdave, I've just realised which +1 you are talking about - that seems to help...

Thanks,
Uni
0

Author Commented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.