SliderPreference: Fix crash when rounding value

This commit is contained in:
Patryk Michalik
2021-03-25 11:14:43 +01:00
parent ce940a606b
commit f033534b80
2 changed files with 17 additions and 2 deletions
@@ -7,7 +7,7 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import java.text.DecimalFormat
import app.lawnchair.util.round
import kotlin.math.roundToInt
@Composable
@@ -37,7 +37,7 @@ fun SliderPreference(
) {
Text(
text = if (showAsPercentage) {
"${(DecimalFormat("#.#").format(value).toFloat() * 100).toInt()}%"
"${(value.round(2) * 100).toInt()}%"
} else {
"${value.roundToInt()}"
}
+15
View File
@@ -0,0 +1,15 @@
package app.lawnchair.util
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
fun Float.round(decimals: Int): Float {
val symbols = DecimalFormatSymbols()
symbols.decimalSeparator = '.'
val format = DecimalFormat()
format.decimalFormatSymbols = symbols
format.applyPattern(
"#.${"#".repeat(decimals)}"
)
return format.format(this).toFloat()
}