Unimatrix_001
asked on
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
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
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
ASKER
Should you subtract from the minimum value or add to the minimum value?Don't have a clue...
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 additionThat is one solution, but it seems I'm over complicating something which seems very simple...
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.
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.
ASKER
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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
place = (1-x%) * AvailableNumbersInRange
Value = max - place
So for your example above:
Place = (1 - 1) * 100
Place = 0
Value = 99 - 0
Value = 99
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
>>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
ASKER
Sorry superdave, I've just realised which +1 you are talking about - that seems to help...
Thanks,
Uni
Thanks,
Uni
ASKER
:)
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