PDA

View Full Version : Resolved jQuery, make negative values positive



Snookerman
12-23-2008, 09:51 PM
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!

jscheuer1
12-23-2008, 10:15 PM
No special jQuery routine is required for that. In place of (which I assume doesn't work, even in jQuery):


difference = makePositive(difference)

Use:


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).

Snookerman
12-23-2008, 10:28 PM
Thanks John! That is exactly what I was looking for.