Click here to Skip to main content
15,887,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code is
Int64 i = ((Int64)Math.Round(Convert.ToDecimal(dr["CHARGE"]) / 10,1,MidpointRounding.AwayFromZero)) * 10;


its working but one exception is
For 100's (eg: 100,200,300 etc), if we set charge as 105,205,etc its not rounding properly. Expected result: 110,120,130 etc, but now its displaying as 100,200,300 etc.

how can avoid this error ?

this is my requirement

95 to 104.9 --> 100
105 to 114.9 --> 110

Any answer is highly appreciated. thanks in advance.
Posted
Updated 9-Jan-13 21:13pm
v2

C#
if (Convert.ToDecimal(x % 10) == 5)
        {
            x = x + 1;


        }
        Int64 i = ((Int64)Math.Round(Convert.ToDecimal(x) / 10)) * 10;
 
Share this answer
 
C#
var ii = Math.Round(Convert.ToDecimal((104.9 + 0.0000001) / 10.00)) * 10 ;
 
Share this answer
 
Just add five, and round:

C#
public int round(float value)
{
   return (((int)value + 5) / 10) * 10;
}
 
Share this answer
 
Try:
C#
public int RoundOff (Decimal valueToRound)
{
   return ((int)Math.Round(valueToRound / 10.0)) * 10;
}
 
Share this answer
 
v2
Try rounding with below line.

C#
Int64 i = ((Int64)Math.Round(Convert.ToDecimal(dr["CHARGE"])/ 10,MidpointRounding.AwayFromZero)) * 10;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900