开发者

Appending a number to a number?

开发者 https://www.devze.com 2023-02-15 05:29 出处:网络
How cold I do the following: say I have the number 10 and would like开发者_开发问答 to append the number 317 to it. The resulting integer would be 10317. How can this be done. Also, once I have this

How cold I do the following:

say I have the number 10 and would like开发者_开发问答 to append the number 317 to it. The resulting integer would be 10317. How can this be done. Also, once I have this number, how could I then for example remove the 17 off the end. Without using strings, and without obvious solving and adding.

Thanks


This will append both numbers

int append_a_and_b_as_int(int a, int b)
{
    for(int tmp = b; tmp > 0; tmp % 10)
    {
        a *= 10;
    }
    return a + b;
}

This will get rid of the last n numbers

int remove_n_numbers_from_a(int n, int a)
{
    for(int i = 0; i < n; i++)
    {
        a /= 10;
    }
    return a;
}


Appending :

int foo(int a, int b)
{
    return a*pow(10, floor(log10(b))+1)+b;
}

Removing :

int bar(int a, int b)
{
    return a/pow(10, floor(log10(b))+1);
}


For the first one:

int a = 10;
int b = 317;
int result = a * 1000 + b;

For the second:

int result2 = result / 100;


If this is something you need to do in your workplace I'd advise quitting.

You can treat your two numbers as numeric data and solve the question, or you can treat them as strings and use an append.

0

精彩评论

暂无评论...
验证码 换一张
取 消