How to roundoff the digits in c# ?

i have to roundoff the numbers to next 5 and next 10

95.46 should be 95.45
95.44 should be 95.45

95.47 should be 95.50
95.49 should be 95.50

95.21 should be 95.20
95.22 should be 95.25

95.19 should be 95.20
VarshiniAsked:
Who is Participating?
 
DhaestConnect With a Mentor Commented:
What I don't understand is what you mean with the next 5  ...
In my opinion the next 5 is always a number higher (or same) as you provided.



code found at http://stackoverflow.com/questions/1329426/how-do-i-round-to-the-nearest-0-5
myNumberTest = RoundUpToNearest(95.46, 0.05);
            myNumberTest = RoundUpToNearest(95.44, 0.05);


     public static Double RoundUpToNearest(Double passednumber, Double roundto)    
     {                 
         if (roundto == 0)        
         {             
             return passednumber;         
         }         
         else         
         {             
             return Math.Ceiling(passednumber / roundto) * roundto;         
         }     
     }     
        public static Double RoundDownToNearest(Double passednumber, Double roundto)     
        {    
            if (roundto == 0)        
            {             
                return passednumber;         
            }         else        
            {             
                return Math.Floor(passednumber / roundto) * roundto;        
            }    
        }

Open in new window

0
 
VarshiniAuthor Commented:
using c#
0
 
mayank_joshiCommented:
i you have to values only between 1 and 100 then you can try:-

Format((Math.Round((val1 + 0.01) * 20) / 20), "0.00")

Open in new window

0
 
mayank_joshiCommented:
here val1 is the value you have to round
0
 
mayank_joshiCommented:
the above code will work correctly for rounding all decimal nos upto 2 decimal places.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.