Given a decimal '96.154', how can I ensure that it is always rounded up to 96.16 (as opposed to normal rounding to 2 decimals which would give 96.15).
Asked
Active
Viewed 1.4k times
17
-
How does 96.154 round up to 96.16 and not round down to 96.15? It would be good to detail the specification for how rounding should work – Russ Cam Aug 03 '12 at 11:14
-
Check out this link http://stackoverflow.com/questions/11740989/rounding-decimal-value/11741129#11741129 – sabz23 Aug 03 '12 at 11:15
-
Is this the actual decimal type, or double? – ken2k Aug 03 '12 at 11:18
-
3Do you care about negative number? Should they be rounded up (toward zero) as well? – Michael Graczyk Aug 03 '12 at 11:18
5 Answers
19
Kind of hacky but a very intuitive way to do so:
var val = 96.154M;
var result = Math.Ceiling(val * 100) / 100.0M;
7
You can add 0.005 to the value and then round the result.
Bruno Ferreira
- 932
- 9
- 22
-
1genius but, depends if you always want to round away from zero, what about negatives? – Jodrell Aug 03 '12 at 11:26
-
@Jodrell The title of the question says it all "always up". By the way, I upvoted the comment about negative numbers. – Bruno Ferreira Aug 03 '12 at 11:27
-
4This will not work when the value is `96.150`, since that would round it to `96.16`, which is incorrect, since that it should stat `96.15`. – Steven Aug 03 '12 at 11:45
6
I think your looking for the Math.Ceiling method.
You could combine this with a multiplier to specify how many decimal places to round. Like this,
public float roundUp(float number, int numDecimalPlaces)
{
double multiplier = Math.Pow(10, numDecimalPlaces))
return Math.ceiling(number*multiplier) / multiplier;
}
Ben Ripley
- 2,025
- 21
- 30
0
Here is the code of a roundUp method for a value and base fraction. The base fraction you should use for your question is 0.05M. However the method can be used for other common scenario which is base fraction 0.5M; And you can apply it in interesting ways like for example using a base fraction of 0.3M. Well I hope it should answer your questions, have fun :
static decimal roundUp(decimal aValue, decimal aBaseFraction)
{
decimal quotient = aValue / aBaseFraction;
decimal roundedQuotient = Math.Round(quotient, 0);
decimal roundAdjust = 0.0M;
if (quotient > roundedQuotient)
{
roundAdjust = aBaseFraction;
}
return roundAdjust + roundedQuotient * aBaseFraction;
}
Jorge Gonzalez
- 1
- 1
0
Here is my version of a RoundUp method, In this can specific decimal
void Main()
{
Console.WriteLine(RoundUp(2.8448M, 2));
//RoundUp(2.8448M, 2).Dump();
}
public static decimal RoundUp(decimal numero, int numDecimales)
{
decimal valorbase = Convert.ToDecimal(Math.Pow(10, numDecimales));
decimal resultado = Decimal.Round(numero * 1.00000000M, numDecimales + 1, MidpointRounding.AwayFromZero) * valorbase;
decimal valorResiduo = 10M * (resultado - Decimal.Truncate(resultado));
if (valorResiduo < 5)
{
return Decimal.Round(numero * 1.00M, numDecimales, MidpointRounding.AwayFromZero);
}
else
{
var ajuste = Convert.ToDecimal(Math.Pow(10, -(numDecimales + 1)));
numero += ajuste;
return Decimal.Round(numero * 1.00000000M, numDecimales, MidpointRounding.AwayFromZero);
}
}