Kiểu dữ liệu Số trong Dart
🎯 Mục tiêu: Hiểu int, double, num và các phép toán số học.
💡 Các kiểu số
int age = 25; // Số nguyên
double height = 1.75; // Số thực
num value = 42; // int hoặc double📝 Định dạng số
int million = 1_000_000; // Underscore separator
int hex = 0xFF; // Hexadecimal
double sci = 2.5e3; // 2500.0🔧 Phép toán số học
int a = 10, b = 3;
print(a + b); // 13
print(a - b); // 7
print(a * b); // 30
print(a / b); // 3.333... (double!)
print(a ~/ b); // 3 (integer division)
print(a % b); // 1 (modulo)🎯 Math library
import 'dart:math';
print(sqrt(16)); // 4.0
print(pow(2, 10)); // 1024
print(sin(pi / 2)); // 1.0
print(Random().nextInt(100)); // 0-99🛠️ Thực hành
void main() {
double price = 99.99;
int quantity = 3;
double discount = 0.1;
// TODO: Tính tổng tiền sau giảm giá
}Lời giải:
void main() {
double price = 99.99;
int quantity = 3;
double discount = 0.1;
double total = price * quantity * (1 - discount);
print("Total: \$${total.toStringAsFixed(2)}");
// Total: $269.97
}✅ Checklist
- Phân biệt
int,double,num - Sử dụng
~/cho integer division - Import
dart:mathcho math functions
Tiếp theo: Boolean
Last updated on