dart数字类型 - int/num/double
场景 当我们需要一个数字类型的时候,需要使用int/num/double
区别 int-整数数字 num-可整可小数 double-小数
1
2
3
4
5
6
7
8
| void main() {
int friendCount = 5; // 整数
print("我有 $friendCount 个朋友");
num rest = 1.5; // 可整数可小数
print("我有 $rest 月的假期");
double appleCount = 1.5; // 小数
print("我买了 $appleCount 个苹果");
}
|
double 不能直接给int赋值 (把需赋值处理 放最后面)
1
2
3
| friendCount // int
appleCount // double
friendCount = appleCount.toInt(); // double 转换为 int 进行赋值
|
num 不能直接给double赋值
1
2
3
| appleCount // double
rest // num
appleCount = rest.toDouble(); // num 转换为 double 进行赋值
|
double 可直接能给num赋值
1
2
3
| appleCount // double
rest // num
rest = appleCount; // double 可以直接给 num 赋值
|