Results 1 to 3 of 3

Thread: jQuery, make negative values positive

  1. #1
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default jQuery, make negative values positive

    Hi, I'm using jQuery and I need to display the difference between two values. The values change so if I just take value1 minus value2, the results will sometimes be negative, but I need the difference which is of course always positive.

    I know I could use conditional statements, like if value1 is larger than value2, show value1 minus value2, but I'm sure there is a better way, something like difference(value1, value2) or var difference = makePositive(difference);

    Thanks for your help!
    Last edited by Snookerman; 12-23-2008 at 10:33 PM. Reason: Resolved

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    No special jQuery routine is required for that. In place of (which I assume doesn't work, even in jQuery):

    Code:
    difference = makePositive(difference)
    Use:

    Code:
    difference = Math.abs(difference);
    The Math Object's abs() method returns the numeric value (of a number object) as its positive value, regardless of its positive or negative status before that.

    To be on the safe side, make sure you are dealing with a number, not a string, before applying (in most cases this will not be an issue).
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Snookerman (12-23-2008)

  4. #3
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    Thanks John! That is exactly what I was looking for.

Tags for this Thread

Bookmarks

Posting Permissions

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