Bỏ qua để đến nội dung

Kiểu dữ liệu Số (int, double, num)

🎯 Mục tiêu: Hiểu int, double, num và các phép toán số học.


int age = 25; // Số nguyên
double height = 1.75; // Số thực
num value = 42; // int hoặc double

int million = 1_000_000; // Underscore separator
int hex = 0xFF; // Hexadecimal
double sci = 2.5e3; // 2500.0

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)

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

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
}

  • Phân biệt int, double, num
  • Sử dụng ~/ cho integer division
  • Import dart:math cho math functions

Tiếp theo: Boolean