Click here to Skip to main content
15,921,203 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
tot = int.Parse(tot + amtusd).ToString();

i want addition not concatenation.
Posted
Updated 17-Apr-11 23:17pm
v3

Try this,
tot = (Convert.ToInt32(amtusd) + Convert.ToInt32(tot)).ToString();

or
tot = (int.Parse(amtusd) + int.Parse(tot)).ToString();
 
Share this answer
 
v2
Comments
Tarun.K.S 18-Apr-11 5:48am    
You too have a 5!
Toniyo Jackson 18-Apr-11 5:50am    
Thanks Tarun
Try this :

C#
string tot = (int.Parse(tot) + int.Parse(amtusd)).ToString();
 
Share this answer
 
Comments
Toniyo Jackson 18-Apr-11 5:26am    
Good answer. I was late. +5
Tarun.K.S 18-Apr-11 5:48am    
Thanks!
One of the ways:
C#
 // Sample addition of string and a number
string concatenatedString = String.Empty;
string someString1 = "StringABC";
string someString2 = "1234";
int someNumber = 678;
concatenatedString =  someString1 + someString2 + someNumber.ToString();

Now, couple of strings if concatenated is fine like this. If you want to concatenate lots of them, then you should use StringBuilder[^].

UPDATE:
If numbers (in string format) to be added and then again converted to string then:
for string to numbers, read: Int.Parse or Convert.ToInt32
for converting numbers to string, read: ToString() method.
 
Share this answer
 
v2
Comments
Tarun.K.S 18-Apr-11 5:23am    
I didn't downvote this Sandeep but OP wants to add the string representation of numbers(tot and amtusd) and display it as string. Though OP didn't explain much.
Sandeep Mewara 18-Apr-11 5:28am    
Thats ok. I don't care of downvoters. their wish.

About what you said: OP wants to add the string representation of numbers(tot and amtusd) and display it as string.
Did you see that I gave a generic example of adding both numbers and string. If OP cannot deduce it just to a number then i doubt if someone can help!
Sandeep Mewara 18-Apr-11 5:29am    
+ too confusing on what he is looking for. badly formed question. :)
Tarun.K.S 18-Apr-11 5:52am    
It was indeed confusing.
Sandeep Mewara 18-Apr-11 5:54am    
Yet, based on what you said, I updated my answer.
Hi,

Firstly, are you wanting to add int together, or double? or decimal?

I will base it on Double, (that way it will kinda do both)

C#
public static double? ConvertToDouble(string p_dblToConvert)
      {
          try
          {
              return double.Parse(p_dblToConvert.Trim());
          }
          catch (Exception)
          {
              return null;
          }
      }
private static string AdditAndReturnIt(string p_no1, string p_no2)
      {
          double? no1conv = ConvertToDouble(p_no1);
          double? no2conv = ConvertToDouble(p_no2);
          if ((no1conv != null) & (no2conv != null))
          {
              return (no1conv + no2conv).ToString();
          }
          return "";
      }



Using it, I just dragged 3 textboxes and a button to a form, and on button click event...

C#
private void button1_Click(object sender, EventArgs e)
       {
           string no1 = textBox1.Text;
           string no2 = textBox2.Text;
           textBox3.Text = AdditAndReturnIt(no1, no2);
       }



Hope this is what you were thinking

Good luck
 
Share this answer
 
Comments
Tarun.K.S 18-Apr-11 5:51am    
You could have used Double.TryParse. It will return null if the conversion failed.
Rob Branaghan 18-Apr-11 5:55am    
Thanks for your input. Will keep that in mind.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900