Hay how do i covert a negative float (like -4.00) to a positive? (like 4.00).
Thanks
Hay how do i covert a negative float (like -4.00) to a positive? (like 4.00).
Thanks
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
Do you want to take the absolute value ( Math.abs(x) ) or simply flip the sign ( x * -1.00 )
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);