添加了注释,完善了参数校验及空值处理
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user