Dart num sign
Syntax & Examples


num.sign property

The `sign` property in Dart returns the sign of a number as minus one, zero, or plus one.


Syntax of num.sign

The syntax of num.sign property is:

 num sign 

This sign property of num returns minus one, zero or plus one depending on the sign and numerical value of the number.

Return Type

num.sign returns value of type num.



✐ Examples

1 Getting the sign of integer numbers

In this example,

  1. We define three integer variables, number1, number2, and number3, with values -5, 0, and 10 respectively.
  2. We use the sign property to get their signs and store the results in sign1, sign2, and sign3.
  3. We print the results to standard output.

Dart Program

void main() {
  num number1 = -5;
  num number2 = 0;
  num number3 = 10;
  num sign1 = number1.sign;
  num sign2 = number2.sign;
  num sign3 = number3.sign;
  print('Sign of $number1: $sign1');
  print('Sign of $number2: $sign2');
  print('Sign of $number3: $sign3');
}

Output

Sign of -5: -1
Sign of 0: 0
Sign of 10: 1

2 Getting the sign of decimal numbers

In this example,

  1. We define two decimal variables, number4 and number5, with values -7.5 and 4.9 respectively.
  2. We use the sign property to get their signs and store the results in sign4 and sign5.
  3. We print the results to standard output.

Dart Program

void main() {
  num number4 = -7.5;
  num number5 = 4.9;
  num sign4 = number4.sign;
  num sign5 = number5.sign;
  print('Sign of $number4: $sign4');
  print('Sign of $number5: $sign5');
}

Output

Sign of -7.5: -1
Sign of 4.9: 1

Summary

In this Dart tutorial, we learned about sign property of num: the syntax and few working examples with output and detailed explanation for each example.