添加净值率判断
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 (startsWith(".")) return false
|
||||||
|
|
||||||
// 处理前导零的情况
|
// 处理前导零的情况
|
||||||
if (length > 1 && startsWith("0")) {
|
if (length > 1 && startsWith("0")) {
|
||||||
// 允许 "0" 和 "0.x" 但不允许 "00", "01", "00.1" 等
|
// 允许 "0" 和 "0.x" 但不允许 "00", "01", "00.1" 等
|
||||||
@@ -81,9 +83,14 @@ fun String.isValidFloat(): Boolean {
|
|||||||
return false
|
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 {
|
fun String.toSafeFloat(maxDecimalDigits: Int = 2): Float {
|
||||||
|
|||||||
@@ -85,10 +85,10 @@ data class GoodsAddParam(
|
|||||||
|
|
||||||
val netRateStr: String
|
val netRateStr: String
|
||||||
get() {
|
get() {
|
||||||
if (netRate == null || netRate == 0f) {
|
return if (netRate == null || netRate == 0f) {
|
||||||
return ""
|
""
|
||||||
} else {
|
} else {
|
||||||
return netRate!!.toFormattedString()
|
netRate!!.toFormattedString(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -383,10 +383,11 @@ private fun DialogRightEditView(viewModel: SelfProcurementViewModel) {
|
|||||||
.width(474.dp),
|
.width(474.dp),
|
||||||
label = "净材率",
|
label = "净材率",
|
||||||
value = goodsAddParam.netRateStr,
|
value = goodsAddParam.netRateStr,
|
||||||
inputType = InputType.Decimal,
|
inputType = InputType.Percent,
|
||||||
onValueChange = {
|
onValueChange = {
|
||||||
viewModel.updateGoodsAddParam(goodsAddParam.copy(netRate = it.toSafeFloat()))
|
viewModel.updateGoodsAddParam(goodsAddParam.copy(netRate = it.toSafeFloat()))
|
||||||
})
|
}, trailingLabel = "%"
|
||||||
|
)
|
||||||
ColumnInputText(
|
ColumnInputText(
|
||||||
modifier = Modifier.width(474.dp),
|
modifier = Modifier.width(474.dp),
|
||||||
value = goodsAddParam.unitIdStr,
|
value = goodsAddParam.unitIdStr,
|
||||||
@@ -498,6 +499,9 @@ fun ColumnInputText(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加物品弹窗
|
||||||
|
*/
|
||||||
@Composable
|
@Composable
|
||||||
fun AddProductDialog(
|
fun AddProductDialog(
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
|
|||||||
@@ -83,7 +83,8 @@ import timber.log.Timber
|
|||||||
enum class InputType {
|
enum class InputType {
|
||||||
Text, // 字符串
|
Text, // 字符串
|
||||||
Number, // 数字
|
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 -> {
|
else -> {
|
||||||
inputValue = newValue
|
inputValue = newValue
|
||||||
onEditingComplete(true)
|
onEditingComplete(true)
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.sw.inbound
|
package com.sw.inbound
|
||||||
|
|
||||||
import org.junit.Assert.assertEquals
|
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -9,8 +8,52 @@ import org.junit.Test
|
|||||||
* See [testing documentation](http://d.android.com/tools/testing).
|
* See [testing documentation](http://d.android.com/tools/testing).
|
||||||
*/
|
*/
|
||||||
class ExampleUnitTest {
|
class ExampleUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun addition_isCorrect() {
|
fun addition_isCorrect() {
|
||||||
assertEquals(4, 2 + 2)
|
// println(4, 2 + 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun log(tag: String, msg: Boolean) {
|
||||||
|
println("$tag, result = $msg")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testFloat() {
|
||||||
|
log("0.00", "0.00".isValidFloat())
|
||||||
|
log("0.01", "0.01".isValidFloat())
|
||||||
|
log("00", "00".isValidFloat())
|
||||||
|
log("01", "01".isValidFloat())
|
||||||
|
log("10", "10".isValidFloat())
|
||||||
|
log("10.00", "10.00".isValidFloat())
|
||||||
|
log("10.01", "10.01".isValidFloat())
|
||||||
|
log("10.011", "10.011".isValidFloat())
|
||||||
|
log("0.0-1", "0.0".isValidFloat(1))
|
||||||
|
log("10.011-1", "10.011".isValidFloat(1))
|
||||||
|
log("10.01-1", "10.01".isValidFloat(1))
|
||||||
|
log("10.0-1", "10.0".isValidFloat(1))
|
||||||
|
log("10.0-0", "10.0".isValidFloat(0))
|
||||||
|
log("10.01-0", "10.01".isValidFloat(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
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" 等
|
||||||
|
if (!startsWith("0.")) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建动态正则表达式
|
||||||
|
val decimalRegex = when {
|
||||||
|
maxFractionDigits <= 0 -> Regex("^\\d+$") // 不允许小数
|
||||||
|
else -> Regex("^\\d+\\.?\\d{0,${maxFractionDigits}}$")
|
||||||
|
}
|
||||||
|
|
||||||
|
return matches(decimalRegex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user