Click here to Skip to main content
15,887,304 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how do I round integer number ,
for example 2,548,250 ===> 2,549,000
Posted

You could create a method for doing that, like in the next example:
C#
public int RoundInt(int number, int digits)
        {
            int factor = (int)Math.Pow(10, digits);
            //
            if (number / factor > 0)
            {
                double temp = Math.Round((double)number/ factor, 0);
                return (int)temp * factor;
            }
            else
            {
                return 0;
            }
        }


Then you could invoke it like in the next example:
C#
int result = RoundInt(123,567 , 3); //==> 124,000 !
 
Share this answer
 
v2
Comments
Mehdi Gholam 9-Dec-14 2:59am    
There is a simpler way, see my answer, 5'ed!
Raul Iloc 9-Dec-14 4:33am    
Yes, your solution is using a single line of code, but you should put it into a function with params.
vajihe.mirzaei 9-Dec-14 3:01am    
this answer isn't increasing .
please write a method that ...
Praveen Kumar Upadhyay 9-Dec-14 3:09am    
This is not working too. it is simply trimming the last 3 digits.
Raul Iloc 9-Dec-14 4:31am    
Sorry my fault, now I updated my solution and it works!
Very simply where X is your number :
C#
int result = (int)(Math.Ceiling((double)X / 1000) * 1000);
 
Share this answer
 
Comments
Praveen Kumar Upadhyay 9-Dec-14 3:07am    
It's not working. Same result.
Mehdi Gholam 9-Dec-14 3:10am    
I doubt that. Check again.
Praveen Kumar Upadhyay 9-Dec-14 3:16am    
Yes now it is working now. 5+
BillWoodruff 9-Dec-14 3:16am    
+5
Mehdi Gholam 9-Dec-14 3:16am    
Cheers Bill!

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