RedDragon2001
RedDragon2001

Reputation: 129

How to get the value from slider in other format?

currently my slider looks like this enter image description here

I want to change the number of current position in a format without decimal place. So in this case it should show me 27 instead of 0.2731...

here is my Code

@Composable
fun Slider() {
    var sliderPosition by remember { mutableStateOf(0f) }
    Slider(value = sliderPosition,
        onValueChange = { sliderPosition = it })
    Text(text = sliderPosition.toString())
}

Upvotes: 2

Views: 4576

Answers (2)

ITmindCo
ITmindCo

Reputation: 148

Convert to rounded int and then to float

Slider(
    value = sliderPosition,
    onValueChange = { sliderPosition = it.roundToInt().toFloat() },
    valueRange = 0f..100f
)

Upvotes: 8

Thracian
Thracian

Reputation: 67393

Slider has a valueRange paremeter which is valueRange: ClosedFloatingPointRange<Float> = 0f..1f by default, you can change it as

valueRange = 0f..100f or valueRange = 0f..360f or any closed range of your choosing

 Slider(
        value = sliderPosition,
        onValueChange = { sliderPosition = it},
        valueRange = 0f..100f
    )

Upvotes: 1

Related Questions