Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 9-Jan-13 22:00pm
Edited 9-Jan-13 22:13pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try:
public int RoundOff (Decimal valueToRound)
{
   return ((int)Math.Round(valueToRound / 10.0)) * 10;
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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

        }
        Int64 i = ((Int64)Math.Round(Convert.ToDecimal(x) / 10)) * 10;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

var ii = Math.Round(Convert.ToDecimal((104.9 + 0.0000001) / 10.00)) * 10 ;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Just add five, and round:
 
public int round(float value)
{
   return (((int)value + 5) / 10) * 10;
}
 
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try rounding with below line.
 
Int64 i = ((Int64)Math.Round(Convert.ToDecimal(dr["CHARGE"])/ 10,MidpointRounding.AwayFromZero)) * 10;
  Permalink  

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

  Print Answers RSS
0 Peter Leow 338
1 Kornfeld Eliyahu Peter 230
2 OriginalGriff 223
3 Maciej Los 210
4 Sergey Alexandrovich Kryukov 152
0 OriginalGriff 383
1 Peter Leow 318
2 Kornfeld Eliyahu Peter 260
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 212


Advertise | Privacy | Mobile
Web01 | 2.8.150301.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100