Results 1 to 5 of 5

Thread: Absolute value of numbers

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Posts
    76

    Absolute value of numbers

    I am trying to use the Abs function to return the absolute value of a range of numbers. The range includes both positive and negative numbers. The numbers are also being formatted as currency. The code I'm using is returning the value of the positive numbers minus the value of the negative numbers.
    VB Code:
    1. lblFinImp_r.Caption = Format(Abs(CCur(txtTmpVal1 + txtTmpVal2)), "$#,##.##")
    What am I doing wrong?

  2. #2
    Addicted Member
    Join Date
    Jan 2003
    Posts
    163
    your code says to take txtTempVal1 and add it to txtTempVal2, after tht has been done, convert it to currency, after that has been done take the abs and then format it - if you want the abs of each item then take the abs of each item prior to the add

  3. #3
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    "absolute value" is a math concept which is applied to a single number, so what do you MEAN when you say you want the absolute value of a range?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Personally, I'm a bit fuzzy on what the problem is..... Do you get an error? Are you getting in correct result? What?
    Syntatically, what you've done looks right, but it depends on what you are trying to accomplish.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    FinChase is either getting a type mismatch error or incorrect results. It depends on the values of the textboxes (assuming txtTmpVal1 and txtTmpVal2 are TextBoxes)

    Adding two strings is not addition in the math sense, its the concatenation if two values. Even if the values are numeric. So, if

    txtTmpVal1 = 1 and txtTmpVal2 = -2 then (txtTmpVal1 + txtTmpVal2) = "1-2". The CCur("1-2") will result in a type mismatch.

    if
    txtTmpVal1 = 3 and txtTmpVal2 = 2 then (txtTmpVal1 + txtTmpVal2) = "32" - which is an incorrect result as it should be 5.

    Convert each textbox value individually.

    <on soap box>
    Just curious (again assuming txtTmpVal1 is a textbox). Why did you rely on the default property of the textbox and not code txtTmpVal1.Text, yet for the Label you specified the default property (.Caption). When coding try to be consistent, do it one way or the other but don't mix and match. Sorry, it just drives me nuts.
    <off soap box>

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