Home All Groups Group Topic Archive Search About

negative int to positive int value

Author
25 Mar 2005 9:21 AM
bardo
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

Author
25 Mar 2005 9:57 AM
Bob Powell [MVP]
(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.





Show quote
"bardo" <ba***@discussions.microsoft.com> wrote in message
news:8DF03CC6-5045-4ABC-893E-F78D256F2962@microsoft.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
Author
25 Mar 2005 10:02 AM
Stephany Young
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


Show quote
"bardo" <ba***@discussions.microsoft.com> wrote in message
news:8DF03CC6-5045-4ABC-893E-F78D256F2962@microsoft.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
Author
25 Mar 2005 10:42 AM
Till Meyer
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
Author
25 Mar 2005 11:03 AM
bardo
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

AddThis Social Bookmark Button