Hasen
Hasen

Reputation: 12344

Equivalent for Javascript's Math.sign but for Dart

The Math.sign() function returns either a positive or negative +/- 1, indicating the sign of a number passed into the argument.. If the number passed into Math.sign() is 0, it will return a +/- 0. Note that if the number is positive, an explicit (+) will not be returned.

Can this be done in dart - perhaps with the Maths library? I searched but couldn't find much about it.

Upvotes: 7

Views: 945

Answers (1)

Hasen
Hasen

Reputation: 12344

Not sure what to do with this question, either delete it or answer it. The solution as provided by pskink is to use num.sign which does not require the Dart Maths library.

It's usage is as follows:

var a = 5;
var b = -32;
var c = a.sign; //without ()
var d = b.sign;
print(c); // 1;
print(d); // -1;

and not sign(b) as I was using it.

Upvotes: 8

Related Questions