Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

format as a positive number regardless if pos or neg 1

Status
Not open for further replies.

tester321

Programmer
Mar 13, 2007
150
CA
Hi i have a number that sometime might be negative, but that screws up the senerio, so for a plave where there might be a negative number i would like to covert it to a positive, this is what i have which isn't working :(


Response.Write( (int)Math(Abs(Convert.ToInt32(listpc[index].val))) );

so if i the array value is -5 i need it to be 5, and if its 5 then stays positive.

Thanks
 
Code:
int[] numbers = new int[] { -2, -1, 0, 1, 2 };
foreach(int number in numbers)
{
   Console.WriteLine(Math.Abs(number));
}
results
[tt]
2
1
0
1
2
[/tt]

-1 is not replaced by 1 Math.Abs() returns a new object with a value of 1, so the array still contains [tt]-2, -1, 0, 1, 2[/tt]

in your case, you can remove the (int) cast. the value of [tt]listpc[index].val[/tt] is still negative, but the result of [tt]Math.Abs(listpc[index].val)[/tt] is a new postive integer

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top