添加净值率判断
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -85,10 +85,10 @@ data class GoodsAddParam(
|
||||
|
||||
val netRateStr: String
|
||||
get() {
|
||||
if (netRate == null || netRate == 0f) {
|
||||
return ""
|
||||
return if (netRate == null || netRate == 0f) {
|
||||
""
|
||||
} else {
|
||||
return netRate!!.toFormattedString()
|
||||
netRate!!.toFormattedString(1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -383,10 +383,11 @@ private fun DialogRightEditView(viewModel: SelfProcurementViewModel) {
|
||||
.width(474.dp),
|
||||
label = "净材率",
|
||||
value = goodsAddParam.netRateStr,
|
||||
inputType = InputType.Decimal,
|
||||
inputType = InputType.Percent,
|
||||
onValueChange = {
|
||||
viewModel.updateGoodsAddParam(goodsAddParam.copy(netRate = it.toSafeFloat()))
|
||||
})
|
||||
}, trailingLabel = "%"
|
||||
)
|
||||
ColumnInputText(
|
||||
modifier = Modifier.width(474.dp),
|
||||
value = goodsAddParam.unitIdStr,
|
||||
@@ -498,6 +499,9 @@ fun ColumnInputText(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加物品弹窗
|
||||
*/
|
||||
@Composable
|
||||
fun AddProductDialog(
|
||||
modifier: Modifier = Modifier,
|
||||
|
||||
@@ -83,7 +83,8 @@ import timber.log.Timber
|
||||
enum class InputType {
|
||||
Text, // 字符串
|
||||
Number, // 数字
|
||||
Decimal // 浮点
|
||||
Decimal, // 浮点
|
||||
Percent, // 百分比
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -147,6 +148,14 @@ fun CustomTextField(
|
||||
}
|
||||
}
|
||||
|
||||
InputType.Percent -> {
|
||||
val range: ClosedFloatingPointRange<Float> = 0f..100f
|
||||
if (newValue.isEmpty() || (newValue.isValidFloat(1) && newValue.toFloat() in range)) {
|
||||
inputValue = newValue
|
||||
onEditingComplete(true)
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
inputValue = newValue
|
||||
onEditingComplete(true)
|
||||
|
||||
Reference in New Issue
Block a user