添加 .add0方法

master
张萌 3 years ago
parent 2c6111afb8
commit e7394e647f

@ -103,6 +103,17 @@ class _ExampleNumExtState extends State<ExampleNumExt> {
child: Container(color: Colors.green),
),
),
ListTile(title: Text('add0')),
ListTile(
leading: Chip(label: Text('.add0')),
title: Text('1'),
subtitle: Text(1.add0(3)),
),
ListTile(
leading: Chip(label: Text('.add0')),
title: Text('1000'),
subtitle: Text(1000.add0(3)),
),
// END
],
);

@ -17,12 +17,15 @@ extension NumExt on num {
BorderRadius get topRadius => BorderRadius.vertical(
top: Radius.circular(this.w),
);
BorderRadius get bottomRadius => BorderRadius.vertical(
bottom: Radius.circular(this.w),
);
BorderRadius get leftRadius => BorderRadius.horizontal(
left: Radius.circular(this.w),
);
BorderRadius get rightRadius => BorderRadius.horizontal(
right: Radius.circular(this.w),
);
@ -57,4 +60,20 @@ extension NumExt on num {
return str;
}
}
///0
///
///
String add0(int num) {
var str = this.toString();
if (str.length >= num) {
return str;
} else {
int dif = num - str.length;
for (var i = 0; i < dif; i++) {
str = '0' + str;
}
return str;
}
}
}

Loading…
Cancel
Save