添加了注释,完善了参数校验及空值处理

This commit is contained in:
zxj
2025-07-09 15:29:52 +08:00
parent de80a26c2c
commit 6cf7b4fb9b
42 changed files with 584 additions and 334 deletions
@@ -33,26 +33,35 @@ private fun Double.roundToDouble(decimalPlaces: Int): Double {
return kotlin.math.round(this * factor) / factor
}
fun Double?.toSafeString(unitName: String?): String {
if (this == null || this == 0.0) return ""
val decimalPlaces = getDecimalPlaces(unitName)
return "%.${decimalPlaces}f".format(this)
}
fun Double?.toSafeDouble(unitName: String?): Double {
if (this == null || this == 0.0) return 0.0
val decimalPlaces = when (unitName) {
"", "", "公斤", "" -> 2
else -> 0
}
val decimalPlaces = getDecimalPlaces(unitName)
return roundToDouble(decimalPlaces)
}
fun Double?.toSafeFloat(unitName: String?): Float {
if (this == null || this == 0.0) return 0f
val decimalPlaces = getDecimalPlaces(unitName)
return roundToDouble(decimalPlaces).toFloat()
}
/**
* 通过单位判断小数
*/
private fun getDecimalPlaces(unitName: String?): Int {
val decimalPlaces = when (unitName) {
"", "", "公斤", "" -> 2
else -> 0
}
return roundToDouble(decimalPlaces).toFloat()
return decimalPlaces
}
fun BigDecimal.toFormattedString(): String {