From e7394e647f1b11fc1e437526e84806dfe637cacf Mon Sep 17 00:00:00 2001 From: zhangmeng <494089941@qq.com> Date: Mon, 23 Aug 2021 19:00:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20.add0=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/lib/extension/example_num_ext.dart | 11 +++++++++++ lib/extension/num_extension.dart | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/example/lib/extension/example_num_ext.dart b/example/lib/extension/example_num_ext.dart index 24a671d..dad37a8 100644 --- a/example/lib/extension/example_num_ext.dart +++ b/example/lib/extension/example_num_ext.dart @@ -103,6 +103,17 @@ class _ExampleNumExtState extends State { 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 ], ); diff --git a/lib/extension/num_extension.dart b/lib/extension/num_extension.dart index d08d602..2fe4131 100644 --- a/lib/extension/num_extension.dart +++ b/lib/extension/num_extension.dart @@ -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; + } + } }