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 08:00

I am not sure if you want round or ceil. But the behavior you show in the question suggests ceil. So I included that.

int my_ceil(int num)
{
    int den = 1;
    int inc = 0;

    while (num >= 10) {
        inc += num % 10;
        num /= 10;
        den *= 10;
    }

    return (num + (inc > 0)) * den;
}

EDIT

Changed the code to remove ceil and other extra operations.

EDIT 2

Fixed for multiples of 10.

查看更多
【Aperson】
3楼-- · 2020-07-06 08:00

No loops.

#include <math.h>
unsigned roundToNextExp10( unsigned a )
{
    int d = a ;
    if( a >= 10 )
    {
        int m ;
        d-- ;
        m = (int)pow( 10, ((int)log10(d)) ) ;
        d = (int)((d / m) + 1) * m ;
    }        
    return d ;
}
查看更多
beautiful°
4楼-- · 2020-07-06 08:01

Avoid string conversions and loops:

int num = ... // your number
int len = log10(num);
float div = pow(10, len);
int rounded = ceil(num / div) * div;
查看更多
爷的心禁止访问
5楼-- · 2020-07-06 08:01

Try taking the first character of the input number, add 1, then append zeros.

    Dim Input = "23568"
    Dim roundUp = Left(Input, 1) + 1

    For x = 1 To Len(Input) - 1
        roundUp &= "0"
    Next

In VB, but hopefully you get the idea.

查看更多
叼着烟拽天下
6楼-- · 2020-07-06 08:01

Some expert advice you get here... To round a number i up to nearest 10:

if (i%10)
   i += 10 - i%10;    

Similarly for 100s, 1000s, etc.

查看更多
Summer. ? 凉城
7楼-- · 2020-07-06 08:08
/*-------------------------------------------------*/
/* Round up a number                               */
/* (Negative numbers go DOWN)                      */
/* 34 -> 40                                        */
/* -34 -> -40                                      */
/*-------------------------------------------------*/
int round_up_10(int num) 
{
    int sign = 1;
    int tens = 1;

    if (num < 0)
    {
        num = (-1) * num;
        sign = -1;
    }
    num = (num + 9) / 10;
    num = num * 10;

    return sign * num;
}
/*-------------------------------------------------*/
/* Round down a number                             */
/* (Negative numbers go UP)                        */
/* 34 -> 30                                        */
/* -34 -> -30                                      */
/*-------------------------------------------------*/
int round_down_10(int num)   
{
    int sign = 1;
    int tens = 1;

    if (num < 0)
    {
        num = (-1) * num;
        sign = -1;
    }
    num = num / 10;
    num = num * 10;

    return sign * num;
}

main()
{
    printf("round_down_10(25450)= %d\n", round_down_10(25450));
    printf("round_down_10(-25450)= %d\n", round_down_10(-25450));
    printf("round_up_10(25450)= %d\n", round_up_10(25450));
    printf("round_up_10(-25450)= %d\n", round_up_10(-25450));

    printf("round_down_10(1347)= %d\n", round_down_10(1347));
    printf("round_down_10(-1347)= %d\n", round_down_10(-1347));
    printf("round_up_10(1347)= %d\n", round_up_10(1347));
    printf("round_up_10(-1347)= %d\n", round_up_10(-1347));

    printf("round_down_10(34)= %d\n", round_down_10(34));
    printf("round_down_10(-34)= %d\n", round_down_10(-34));
    printf("round_up_10(34)= %d\n", round_up_10(34));
    printf("round_up_10(-34)= %d\n", round_up_10(-34));
}
    /*
    The outout:
    round_down_10(25450)= 25450
    round_down_10(-25450)= -25450
    round_up_10(25450)= 25450
    round_up_10(-25450)= -25450
    round_down_10(1347)= 1340
    round_down_10(-1347)= -1340
    round_up_10(1347)= 1350
    round_up_10(-1347)= -1350
    round_down_10(34)= 30
    round_down_10(-34)= -30
    round_up_10(34)= 40
    round_up_10(-34)= -40
    */
查看更多
登录 后发表回答