Task: Add format number with precision and updated for dart shared

This commit is contained in:
Sambo Chea 2021-06-07 17:50:04 +07:00
parent b18c4f01a9
commit 57cded1c79
3 changed files with 18 additions and 0 deletions

View File

@ -0,0 +1,7 @@
import 'package:cubetiq/src/text/functions.dart';
import 'package:cubetiq/src/xlog/xlog.dart';
void main(List<String> args) {
XLog.debug(StringUtils.format(10.5555, 2));
XLog.debug(StringUtils.formatFromString('10.5555', 2));
}

View File

@ -32,4 +32,8 @@ extension StringExtensionOnNullable on String? {
bool get isNullOrEmptyOrBlank => StringUtils.isNullOrEmptyOrBlank(this); bool get isNullOrEmptyOrBlank => StringUtils.isNullOrEmptyOrBlank(this);
bool get isEqualsIgnoreCase => StringUtils.equalsIgnoreCase(this, this); bool get isEqualsIgnoreCase => StringUtils.equalsIgnoreCase(this, this);
String toPrecision([int precision = 2]) {
return StringUtils.formatFromString(this, precision);
}
} }

View File

@ -6,6 +6,13 @@ class StringUtils {
return n.toStringAsFixed(n.truncateToDouble() == n ? precision : precision); return n.toStringAsFixed(n.truncateToDouble() == n ? precision : precision);
} }
/// Format number from String with precision
static String formatFromString(String? text, [int precision = 2]) {
text ??= '0.0';
var n = double.parse(text);
return n.toStringAsFixed(n.truncateToDouble() == n ? precision : precision);
}
/// Text format with custom args /// Text format with custom args
static String? textFormat(String? text, List<dynamic> args) => static String? textFormat(String? text, List<dynamic> args) =>
TextFormatter(text).format(args); TextFormatter(text).format(args);