Click here to Skip to main content
15,881,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I want to convert a negative number to a positive.
the match.abs(dif) does not work.
Is there any other possible way to do this?
When i run the code as is ,if the dif is negative,it stays negative.


VB
Dim Total As Decimal = 0
               Dim currTotal As Decimal = Math.Round(dtreader(1), 2)
               Total = Math.Round(Decimal.Parse((dtreader(2) + dtreader(3)) + dtreader(4)), 2)
               Dim avg As Decimal = Math.Round(Total / 3, 2)
               Dim dif As Decimal = currTotal - Total

               Math.Abs(dif)

               Dim StatisticDiff As Decimal = Math.Round((dif / currTotal) * 100, 2)

               If currTotal > Total Then
                   MsgBox(currTotal & "    : increased by : " & StatisticDiff)
               ElseIf currTotal < Total Then
                   MsgBox(currTotal & "    : decreased by : " & StatisticDiff)
               Else
                   MsgBox(currTotal & "    : stayed the same : " & StatisticDiff)
               End If
Posted
Comments
Kenneth Haugland 4-Oct-12 6:00am    
If number < 0 then number*=-1 end if?

That is quite normal, you should modify your abs line to:
VB
dif = Math.Abs(dif)

Doing that you'll put the absolute value of dif into dif.

See this link[^].

The Math.Abs function returns the absolute value of a number passed as parameter.
In that way you can do plenty of things:
VB
dif = Math.Abs(dif)
another_var = Math.Abs(dif)
var3 = Math.Abs(function_call())
...

Good luck!
 
Share this answer
 
v2
Comments
Member 9374423 4-Oct-12 6:02am    
Perfect thank you
Joan M 4-Oct-12 6:03am    
you are welcome! glad to help.
Kenneth Haugland 4-Oct-12 6:40am    
Thanks, Now I feel stupid... Like a Function instead of a Sub. String.Replace is like that too...
Joan M 4-Oct-12 6:46am    
You shouldn't.
I must say that your method:
A) works
B) I've thought of it before posting and searching google about the ABS function.
;)
Maths.Abs returns a decimal, it doesn't set the value of the supplied parameter.

So, dif = Math.Abs(dif) should fix your problem.
 
Share this answer
 
Math.Abs
Method[^] returns an absolute value that contains the result.
You need to capture the return value.
 
Share this answer
 

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