ToneSnap_FSX_Flutter/lib/utils/num_util.dart
fengshengxiong 422a3f8802 first commit
2024-06-11 11:53:38 +08:00

28 lines
838 B
Dart

// Author: fengshengxiong
// Date: 2024/5/7
// Description: 整数、浮点数工具类
class NumUtil {
static int getInt(num? value) {
if (value == null) return 0;
return value.toInt();
}
static double getDouble(num? value) {
if (value == null) return 0.0;
return value.toDouble();
}
static String formatNum(double? num, {int index = 2}){
if (num == null) return '0';
if((num.toString().length - num.toString().lastIndexOf('.') - 1) < index){
return num.toStringAsFixed(index).substring(0, num.toString().lastIndexOf('.') + index + 1).toString();
}else{
return num.toString().substring(0, num.toString().lastIndexOf('.') + index + 1).toString();
}
}
static double strToDouble(String valueStr, {double defValue = 0.0}) {
return double.tryParse(valueStr) ?? defValue;
}
}