Rounding integers to nearest ten or hundred in C

2020-07-06 07:26发布

I'm trying to think of a function in C that would satisfy the following conditions:

  • It accepts an integer greater than 0 as an argument;
  • It rounds that integer up to the nearest value so that only the first digit is not a zero

For example:

53 comes out as 60..

197 comes out as 200..

4937 comes out as 5000..

Is there a way to do this so that the requirement is satisfied regardless of the number of trailing zeroes?

For example, I understand how I could do it in any individual case. divide 53 by 10 then ceil(), multiply by 10, but I would like one that can handle any value.

Opinions? Ideas?

标签: c rounding
12条回答
够拽才男人
2楼-- · 2020-07-06 07:47

This should do it:

static int
rnd_up(int val)
{
    double e, r;

    e = exp10(trunc(log10((double)val)));
    r = round(((double)val / e) + 0.5);

    return(r * e);
}
查看更多
乱世女痞
3楼-- · 2020-07-06 07:48

You can divide the number by ten until there is only one digit left, then multiply it back to size:

int n = 4937;

int m = 1;
while (n >= 10) {
  n = (n + 9) / 10;
  m *= 10;
}
n *= m;
查看更多
叛逆
4楼-- · 2020-07-06 07:50

It's unnecessary to convert the number to a string and back. You can do this using basic modulo arithmetic and multiplication and division.

Here's a pure numeric solution, hopefully somewhat more efficient in terms of running time:

int round_up_to_max_pow10(int n)
{
    int tmp = n;
    int i = 0;
    while ((tmp /= 10) >= 10) {
        i++;
    }

    if (n % (int)(pow(10, i + 1) + 0.5)) {
        tmp++;
    }

    for (; i >= 0; i--) {
        tmp *= 10;
    }

    return tmp;
}

printf("Original: %d; rounded: %d\n", 4937, round_up_to_max_pow10(4937));
查看更多
Lonely孤独者°
5楼-- · 2020-07-06 07:52

Logarithms are quite helpful here to provide a constant-time answer to the "how many zeros does this have?"

floor(log10(x))= z //the number of zeros

will take the logarithm base 10 and give you the number of zeros that will be in x.

You can then use the C occasional idiom

(A+B-1)/B

to quickly find the ceiling of A/B, which results in the correct leading digit in this way:

zeros = exp10(1,z);
((x+zeros-1)/zeros) * zeros

This is pseudocode but you should get the idea. The key understanding is that logarithms are the way to mathematically determine how many digits a number has.

查看更多
爷的心禁止访问
6楼-- · 2020-07-06 07:55

By Cocoa APIs:

int number=9435;
NSString *string=[NSString stringWithFormat:@"%d",number];
long length=[string length];    
NSString *roundedString=[NSString stringWithFormat:@"%d",([[string substringToIndex:1]intValue]+1)];
while (--length>0) {
    roundedString=[roundedString stringByAppendingString:@"0"];
}
int roundedNumber=[roundedString intValue];
NSLog(@"%d,   %d",number,roundedNumber);

By Typical C style, mathematically:

int i=8517;

int temp=i;
int len,msb;

for (len=0; temp>0; len++) {
    msb=temp;
    temp/=10;
}
msb++;
int multiplier=1;
for (int i=1; i<len; i++) {
    multiplier*=10;
}
 NSLog(@"Rounded : %d",msb*multiplier);
查看更多
Fickle 薄情
7楼-- · 2020-07-06 07:57

I would convert the number to string. Get the length of the string.

Then:

// pseudo code:
divisor = pow(10, len of number string)
answer = ceil (number/divisor) * divisor;
查看更多
登录 后发表回答