添加净值率判断

This commit is contained in:
zxj
2025-07-11 11:14:18 +08:00
parent f92ae70423
commit a88321b7b6
5 changed files with 75 additions and 12 deletions
@@ -72,8 +72,10 @@ fun String.toSafeDouble(): Double {
}
}
fun String.isValidFloat(): Boolean {
fun String.isValidFloat(maxFractionDigits: Int = 2): Boolean {
if (isEmpty()) return false
if (startsWith(".")) return false
// 处理前导零的情况
if (length > 1 && startsWith("0")) {
// 允许 "0" 和 "0.x" 但不允许 "00", "01", "00.1" 等
@@ -81,9 +83,14 @@ fun String.isValidFloat(): Boolean {
return false
}
}
val decimalRegex = Regex("^\\d+\\.?\\d{0,2}$")
return this.matches(decimalRegex)
// return input.matches(Regex("-?\\d+(\\.\\d+)?"))
// 构建动态正则表达式
val decimalRegex = when {
maxFractionDigits <= 0 -> Regex("^\\d+$") // 不允许小数
else -> Regex("^\\d+\\.?\\d{0,${maxFractionDigits}}$")
}
return matches(decimalRegex)
}
fun String.toSafeFloat(maxDecimalDigits: Int = 2): Float {