SliderPreference: Fix crash when rounding value
This commit is contained in:
@@ -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()}"
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user