views:

90

answers:

6

Hay how do i covert a negative float (like -4.00) to a positive? (like 4.00).

Thanks

+5  A: 

Getting the absolute value: Math.abs()

document.write(Math.abs(7.25));     // 7.25
document.write(Math.abs(-7.25));    // 7.25
document.write(Math.abs(null));     // 0
document.write(Math.abs("Hello"));  // NaN
document.write(Math.abs(7.25-10));  // 2.75
Jonathan Sampson
+1  A: 

Take the absolute value: Math.abs(-4.00)

Darin Dimitrov
A: 
var f=-4.00;

if(f<0)
f=-f;
Xinus
+1  A: 

Do you want to take the absolute value ( Math.abs(x) ) or simply flip the sign ( x * -1.00 )

AlexCuse
A: 

If you know the value to be negative (or want to flip the sign), you just use the - operator:

n = -n;

If you want to get the absolute value (i.e. always return positive regardless of the original sign), use the abs method:

n = Math.abs(n);
Guffa
A: 

value * -1

..is the simplest way to convert from negative to positive