Free Programming Website

Free Programming Website www.sourcecodehub.com

Tuesday, October 30, 2012

How to Convert Positive and Negative Numbers in VB 6.0 and VB.NET

This simple post is to show you how to convert a positive number to a negative number, plus to show how to convert a negative number to a positive number. Both Visual Basic 6.0 and Visual Basic.NET are supported using the code below. (with VB 2005, 2008, 2010, 2012 included).
To do this is actually very simple with very basic code. To convert a Negative Number (Example: -100), to a positive number you simply use the ABS function. The ABS function will return the absolute value from an integer number (whether positive or negative). To convert a positive number to a negative is easy as well. Simply multiple the positive number by a negative one (Example: 100 x -1).
Below starts the code for converting a negative number to a positive number.

'
'This code will work just fine with both VB 6.0 and VB.NET.
'
MsgBox(Math.Abs(-91))

The code below will convert a positive number to a negative number.

'
'This code will work just fine with both VB 6.0 and VB.NET.
'
MsgBox(91 * -1)

Thats all there is to it! Hopefully this little tip helped you out. :)
Jason