Convert negative float to positive in JavaScript

How to convert a negative float (e.g. -4.00) to a positive (e.g. 4.00)?

+3
source share
6 answers

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
+7
source

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

+1
source

(Math.abs(x)) (x * -1.00)

+1
var f=-4.00;

if(f<0)
f=-f;
0

, ( ), -:

n = -n;

(.. ), abs:

n = Math.abs(n);
0

* -1

..

-1
source

Source: https://habr.com/ru/post/1727490/


All Articles