القائمة الرئيسية

الصفحات

 Dart Number Methods


Dart Programming - Abs Method

This property is used to return an integer representing the absolute value of a number.

تُستخدم هذه الخاصية لإرجاع عدد صحيح يمثل القيمة المطلقة لرقم.

Syntax

Number.abs()

Example

void main() {  
   var a = -2; 
   print(a.abs()); 
}  

It will produce the following output −.

2

Dart Programming - ceil Method

This property returns the ceiling value, that is the smallest integer greater than or equal to a number.

تُرجع هذه الخاصية قيمة السقف ، أي أصغر عدد صحيح أكبر من أو يساوي الرقم.

Syntax

Number.ceil()

Example

void main() { 
   var a = 2.4; 
   print("The ceiling value of 2.4 = ${a.ceil()}"); 
}  

It will produce the following output −.

The ceiling value of 2.4 = 3 

Dart Programming - compareTo Method

It returns an integer indicating the relationship between the two numbers.

Syntax

Number.compareTo(x)

Parameter

  • x − represents a number.

Return Value

Returns the value −

  • 0 − if the values are equal.

  • 1 − if the current number object is greater than the specified numeric value.

  • -1 − if the current number object is lesser than the specified numeric value.

Example

void main() { 
   var a = 2.4; 
   print(a.compareTo(12)); 
   print(a.compareTo(2.4)); 
   print(a.compareTo(0)); 
} 

It will produce the following output −.

-1 
0 
1

Dart Programming - floor Method

This method returns the largest integer less than or equal to a number.

Syntax

Number.floor()

Return Value

Returns the largest integer less than or equal to a number x.

Example

void main() { 
   var a = 2.9; 
   print("The floor value of 2.9 = ${a.floor()}"); 
} 

It will produce the following output −.

The floor value of 2.9 = 2

Dart Programming - remainder Method

It returns the truncated remainder after dividing the two numbers.

Syntax

Number.remainder(x)

Parameter

  • x − represents a divisor

Return Value

Returns the remainder of a division.

Example

void main() { 
   var a = 10; 
   var b = 17; 
   
   print(a.remainder(2)); 
   print(b.remainder(2)); 
}

It will produce the following output −.

0 
1

Dart Programming - round Method

This method returns the value of a number rounded to the nearest integer.

Syntax

Number.round()

Return Value

Returns the value of a number rounded to the nearest integer.

Example

void main() { 
   double n1 = 12.023; 
   double n2 = 12.89; 
   
   var value = n1.round(); 
   print( value ); 
   
   value = n2.round(); 
   print( value ); 
} 

It will produce the following output −.

12 
13

Dart Programming - toDouble Method

This method returns the double representation of the number's value.

Syntax

Number.toDouble()

Return Value

Returns a double representing the specified Number object.

Example

void main() { 
   int n1 = 2; 
   var value = n1.toDouble(); 
   print("Output = ${value}"); 
} 

It will produce the following output −.

Output = 2.0

Dart Programming - toInt Method

This method returns the integer representation of the number's value.

Syntax

Number.toInt()

Return Value

Returns an int representing the specified Number object.

Example

void main() { 
   double n1 = 2.0; 
   var value = n1.toInt(); 
   print("Output = ${value}"); 
} 

It will produce the following output −.

Output = 2

Dart Programming - toString Method

This method returns the string representation of the number's value.

Syntax

Number.toString()

Return Value

Returns a string representing the specified Number object.

Example

void main() {   
   int n1 = 2;   
   var value = n1.toString();   
   print( value is String ); 
}  

It will produce the following output −.

true

Dart Programming - truncate Method

Returns an integer after discarding any fractional digits.

Syntax

Number.truncate()

Return Value

Returns an int without decimal points.

Example


void main() { 
   double n1 = 2.123; 
   var value = n1.truncate(); 
   print("The truncated value of 2.123 = ${value}"); 
} 

It will produce the following output −.

The truncated value of 2.123 = 2 
هل اعجبك الموضوع :

تعليقات