I would like to round a number that is based on ratio of two values.
The ratio will include values that are greater or less than the original value where
ratio = newValue / originalvalue
When newValue > originalValue I can round to the nearest lower factor using:
double NearestLowerFactor(float value, double factor)
{
return Math.Floor(value / factor) * factor;
}
For example:
When newValue < originalValue I wish to round to the nearest reciprocal of the factor.
Therefore, if the factor is 2 I would like to round based on factors of 1/2, that is 1/2, 1/4, 1/8, 1/16, etc.
For example:
How would I round to the closest lower factor in this case?
Math.Pow(factor, Math.Floor(Math.Log(ratio, factor)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With