Dart Programming - Numbers
Dart numbers can be classified as −
int − Integer of arbitrary size. The int data type is used to represent whole numbers.
double − 64-bit (double-precision) floating-point numbers, as specified by the IEEE 754 standard. The double data type is used to represent fractional numbers
The num type is inherited by the int and double types. The dart core library allows numerous operations on numeric values.
The syntax for declaring a number is as given below −
يمكن تصنيف أرقام Dart على أنها - int - عدد صحيح من الحجم التعسفي. يتم استخدام نوع البيانات int لتمثيل الأعداد الصحيحة. مزدوج - أرقام الفاصلة العائمة ذات 64 بت (الدقة المزدوجة) ، كما هو محدد بواسطة معيار IEEE 754. يتم استخدام نوع البيانات المزدوج لتمثيل الأرقام الكسرية. يتم توارث نوع الرقم بواسطة النوعين int و double. تسمح مكتبة dart core بالعديد من العمليات على القيم الرقمية. صيغة التصريح عن رقم كما هو وارد أدناه -
int var_name; // declares an integer variable double var_name; // declares a double variable
Example
void main() { // declare an integer int num1 = 10; // declare a double value double num2 = 10.50; // print the values print(num1); print(num2); }
It will produce the following output −
10 10.5
Note − The Dart VM will throw an exception if fractional values are assigned to integer variables.
Parsing
The parse() static function allows parsing a string containing numeric literal into a number. The following illustration demonstrates the same −
void main() { print(num.parse('12')); print(num.parse('10.91')); }
The above code will result in the following output −
12 10.91
The parse function throws a FormatException if it is passed any value other than numerals. The following code shows how to pass an alpha-numeric value to the parse() function.
Example
void main() { print(num.parse('12A')); print(num.parse('AAAA')); }
The above code will result in the following output −
Unhandled exception: FormatException: 12A #0 num.parse (dart:core/num.dart:446) #1 main (file:///D:/Demos/numbers.dart:4:13) #2 _startIsolate.<anonymous closure> (dart:isolatepatch/isolate_patch.dart:261) #3 _RawReceivePortImpl._handleMessage (dart:isolatepatch/isolate_patch.dart:148)
تعليقات
إرسال تعليق