• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

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
0
Varshini
Asked:
Varshini
  • 3
1 Solution
 
VarshiniAuthor Commented:
using c#
0
 
DhaestCommented:
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
 
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now