Help with conversion

  • Thread starter Thread starter Guest
  • Start date Start date
Chris said:
Hi,
How can I convert positive integers to negative integers. Eg 1 to -1.

Not to be a smart aleck, but I find multiplying by -1 to be useful. You
can guarantee a negative result by first taking the absolute value, so

Result = Math.Abs(x) * -1

will always return a negative number.
 
Based on performance. Do you think this is fast

TextBox2.Text = (Abs(Convert.ToDecimal(TextBox1.Text)) * -1).ToString()

Thanks
 
Chris said:
Based on performance. Do you think this is fast

TextBox2.Text = (Abs(Convert.ToDecimal(TextBox1.Text)) * -1).ToString()

Thanks

/shrug

Any time you want to do math with text, the text must first be converted to a
numeric form. If you want to represent the numeric result as text, you need
to convert it back into a string. There is no way around this.

If you have code elsewhere to guarantee that the value in TextBox1 will
always be a positive integer, you can dispense with the call to Abs(). I
*think* that explicitly calling the ToType functions improves performance a
bit over having the conversions done implicitly. So yes, your example is
about the best you will be able to do.
 
Back
Top