# Rounding a number to a smaller or bigger size with the same ratio - C#

TAGS :
Viewed: 2 - Published at: a few seconds ago

#### [ Rounding a number to a smaller or bigger size with the same ratio ]

I don't know how to describe it very well (so I can't look it up) but I need help. Lets say I have 100 out of 0 to 200, how do I ratio the number to 0 to 100 so it would be like 50? Simply ratio-ing the number to a smaller or bigger size?

Calculate the ratio between the original maximum and the new maximum:

``````double k = (double)newMax / (double)oldMax
``````

In your example you would end up with the value `0.5`.

Then just multiply the value with the ratio:

``````double newValue = value * k;
``````

If you want an integer value, round it and convert to integer:

``````int newValue = (int)Math.Round(value * k);
``````

In general, when the minimum is not necessarily 0 on either the 'old' and 'new scales', the solution is the following:

ratio = ( oldValue - oldMin ) / ( oldMax - oldMin )

newValue = newMin + ( newMax - newMin ) * ratio

``````double myRatio, maxValue, newValue;