Results 1 to 10 of 10

Thread: Negative to Positive

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2000
    Location
    Illinois, USA
    Posts
    3

    Question

    I just know I'm going to sound completely ignorant, but how does one go about converting a negative integer to a positive?

    Also, is it possible to move a number-only string (like 15) to an integer variable? Example:

    Code:
    Dim Blah as Integer
    Dim BlahString as String
    
    BlahString = "15"
    Blah = BlahString
    Obviously, VB chokes on this with a type mismatch. Any wisdom you could share would be helpful, thanks very much.

    - Evan Sims

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Dim intMyInteger As Integer
    Dim strMyString As String

    strMyString = "-15"

    intMyInteger = Val(strMyString)

    ' Make a postive integer negative or a negative integer positive
    intMyInteger = intMyInteger * -1

    MsgBox intMyInteger

  3. #3
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Code:
    'convert to positive
    If iNum < 0 Then
      iNum = iNum * -1
    End If
    
    'string to integer
    If IsNumeric(stNum) Then
      iNum = CInt(stNum)
    End If
    
    'or another way
    iNum = Val(stNum)
    Iain, thats with an i by the way!

  4. #4
    Guest
    Val() converts a string to a Double, so it's a good idea to use CInt, CLng, CSng, CByte whenever possible because they consume less memory.

    [Edited by Megatron on 08-21-2000 at 01:28 PM]

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Instead of multipling a negative number with -1 (this would turn a positive number into a negative as well) you can use the Abs function (it always returns a positive value):
    Code:
    Dim i As Integer
    i = -3
    i = Abs(i)

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    I agree with Megatron that it's usually better to use the conversion function in most cases but the Val function could be used with any string that begins with a number:
    Code:
    int iNum%
    iNum = Val("  127 01abcdef")
    The above code would return 12701 but would have raised an error if you would have use the CInt function.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    ABS

    ABS is the same as
    Code:
    Number * Sgn(Number)
    while Sgn is the sign +1 or -1 for the number, or 0 for 0.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: ABS

    Originally posted by kedaman
    ABS is the same as
    Code:
    Number * Sgn(Number)
    while Sgn is the sign +1 or -1 for the number, or 0 for 0.
    This is true but calling Sgn and multiply it with Number is slightly slower then calling the Abs function.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Nope, I think it's the other way round, but i did some tests and they were about the same
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    You guys have the negative to positive thing covered very well, using Abs and Sgn; and the conversion functions like Val and Cxxx are the proper way to convert data from one type to another. However, one other thing bears mentioning in reference to one of Evan's original questions: the issue of VB's "evil type conversion". The behavior of VB from version 4 on up pretty much lets you assign anything to anything and if it can make the conversion, it will; you'll only get the "type mismatch" error if the data in fact cannot be converted. Evan's original code should actually work:
    Code:
    Dim Blah as Integer
    Dim BlahString as String
    
    BlahString = "15"
    Blah = BlahString
    "It's cold gin time again ..."

    Check out my website here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width