forked from cubetiq/cubetiq_dart_shared
Task: Add format number with precision and updated for dart shared
This commit is contained in:
parent
b18c4f01a9
commit
57cded1c79
7
example/cubetiq_functions.dart
Normal file
7
example/cubetiq_functions.dart
Normal 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));
|
||||
}
|
@ -32,4 +32,8 @@ extension StringExtensionOnNullable on String? {
|
||||
bool get isNullOrEmptyOrBlank => StringUtils.isNullOrEmptyOrBlank(this);
|
||||
|
||||
bool get isEqualsIgnoreCase => StringUtils.equalsIgnoreCase(this, this);
|
||||
|
||||
String toPrecision([int precision = 2]) {
|
||||
return StringUtils.formatFromString(this, precision);
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,13 @@ class StringUtils {
|
||||
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
|
||||
static String? textFormat(String? text, List<dynamic> args) =>
|
||||
TextFormatter(text).format(args);
|
||||
|
Loading…
Reference in New Issue
Block a user