472,095 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,095 software developers and data experts.

negative int to positive int value

Does anyone know if there is a easy way to change a negitive int value into a
positive int value.

int a = -105 > change this to 105.

With other words is there a command to change to neg sign into pos sign ?

Bardo Versteeg
Nov 17 '05 #1
4 50194
(int)Math(Abs(<value>));

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"bardo" <ba***@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
Does anyone know if there is a easy way to change a negitive int value
into a
positive int value.

int a = -105 > change this to 105.

With other words is there a command to change to neg sign into pos sign ?

Bardo Versteeg

Nov 17 '05 #2
If you mean to change it after a value has already been assigned to 'a':

Negative or Positive --> Positive
a = Math.Abs(a)

Reverse the sign
a = -(a)
or
a = a * -1
"bardo" <ba***@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
Does anyone know if there is a easy way to change a negitive int value
into a
positive int value.

int a = -105 > change this to 105.

With other words is there a command to change to neg sign into pos sign ?

Bardo Versteeg

Nov 17 '05 #3
bardo wrote:
Does anyone know if there is a easy way to change a negitive int value into a
positive int value.

int a = -105 > change this to 105.

With other words is there a command to change to neg sign into pos sign ?


a = a<0 ? -a : a; // if a is negative >>or<< positive

Till
Nov 17 '05 #4
Thanks Bob , Stephany and Till,

Math.abs(<value>) does the trick.
Also thanks for the other tips. a = -(a) is a handy one to remember.

Bardo

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.