How do I round a non-floating point number? e.g. 9107609 down to 91 or to 911, etc.
            Asked
            
        
        
            Active
            
        
            Viewed 116 times
        
    4 Answers
5
            
            
        Divide by 100000 or 10000, respectively. C division rounds towards zero, so the numbers will always be rounded down. To round to the nearest integer, see this question.
- 
                    Is "rounding down" equivalent to truncating the decimal? E.g. 1.9 gets "rounded down" to 1? – Chris Sprague Dec 17 '14 at 18:19
1
            
            
        Something like floor(number/(10^numberofdigitstocut)) or ceil(number/(10^numberofdigitstocut)) would do.
 
    
    
        kos
        
- 510
- 3
- 18
0
            
            
        #include <math.h>
//returns n rounded to digits length
int round_int (int n, int digits)
{
   int d = floor (log10 (abs (n))) + 1; //number of digits in n
   return floor(n / pow(10, d-digits));
}
 
    
    
        Bence Gedai
        
- 1,461
- 2
- 13
- 25
0
            
            
        To round, use a half way addition (if positive, else subtract 10000/2)
int rounded = (9107609 + 10000/2)/10000;
 
    
    
        chux - Reinstate Monica
        
- 143,097
- 13
- 135
- 256
 
     
     
    