Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine 2 integer's text not add them together

Tags:

c#

int

I have two integers, x and y. What I am trying to do, is combine the numbers in both, not add them together. I have tried to do this:

int x = 5;
int y = 10;
sum = x + y;

But that makes the output 15. What I am wondering is if there is any way to combine them, so that the output would be 510.

5 + 10 = 510

That is what I am trying to accomplice.

I know I could do something like this:

int x = 5;
int y = 10;
int sum;
sum = Convert.ToInt32(x.ToString() + y.ToString());

But that seems like a sloppy way to do it. Is there a better way to do this?

Thanks.

like image 255
Dozer789 Avatar asked Jan 25 '14 22:01

Dozer789


People also ask

How do you stick 2 numbers together in Python?

If you want to concatenate a string and a number, such as an integer int or a floating point float , convert the number to a string with str() and then use the + operator or += operator.

How can we add two numbers without adding in Java?

You need to convert the parameter inside the println method into a string literal ,then java compiler would recognize it as a string and will not add two integers.

How do you append numbers in Java?

To concatenate a String and some integer values, you need to use the + operator. Let's say the following is the string. String str = "Demo Text"; Now, we will concatenate integer values.


1 Answers

A little simplier:

int x = 5;
int y = 10;
int sum;
sum = Convert.ToInt32("" + x + y);

Notice that you need convertion in any case. Implicit conversion is used here.

like image 159
Tony Avatar answered Nov 14 '22 21:11

Tony