# Slider::getValue() gives different value that Slider is set to

**URL:** <https://forum.juce.com/t/slider-getvalue-gives-different-value-that-slider-is-set-to/33406>\
**Category:** General JUCE discussion\
**Created:** [May 15, 2019, 8:53pm UTC](https://forum.juce.com/t/slider-getvalue-gives-different-value-that-slider-is-set-to/33406 "2019-05-15T20:53:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [May 15, 2019, 8:53pm UTC](https://forum.juce.com/t/slider-getvalue-gives-different-value-that-slider-is-set-to/33406/1 "2019-05-15T20:53:25Z")

</div>

Hello,  
I have problem, probably with some types mismatch (`double` vs `float` vs `int`).  
My `Slider` range is set by `AudioParameterFloat` from `-24.00f` to `24.00f`, and step is set to `0.01f`.  
And when I set by text box editor value `0`, then in `sliderValueChanged()` when I use:  
`float value = mySlider.getValue();`  
I get `value = -0.000000536441803`

I also tried: `float value = (float)mySlider.getValue();`  
but I get the same result.

Why is that? How to prevent it? Do I need to write my own method that round float to two decimal places after comma?

For any help great thanks in advance.

---

<div class="post-metadata">

**Author:** ![kalukaley](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kalukaley/32/10667_2.png) [@kalukaley](https://forum.juce.com/u/kalukaley)\
**Post date:** [March 22, 2021, 4:02pm UTC](https://forum.juce.com/t/slider-getvalue-gives-different-value-that-slider-is-set-to/33406/2 "2021-03-22T16:02:20Z")

</div>

I’m noticing similar behavior in my app. Have you found an answer to this?

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [March 22, 2021, 4:41pm UTC](https://forum.juce.com/t/slider-getvalue-gives-different-value-that-slider-is-set-to/33406/3 "2021-03-22T16:41:19Z")

</div>

Hmm… Sorry it was long time ago. And I don’t remember exactly what was my code.

But I suppose in that case I had set step directly on `slider` and simultanously on `AudioProcessorParameter` which was attached to slider and there was some missmatch. But I am not sure.

If it’s the case then maybe you should look something like that in your code.

I’ve just checked my current code. And I see I use only parameter to define range and step. And it works for me like that:  
`std::make_unique<AudioParameterFloat>( PARAM_ID, PARAM_NAME, NormalisableRange<float>( PARAM_MIN, PARAM_MAX, PARAM_STEP, PARAM_SKEW), PARAM_DEFAULT)`

---

<div class="post-metadata">

**Author:** ![kalukaley](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kalukaley/32/10667_2.png) [@kalukaley](https://forum.juce.com/u/kalukaley)\
**Post date:** [March 22, 2021, 4:51pm UTC](https://forum.juce.com/t/slider-getvalue-gives-different-value-that-slider-is-set-to/33406/4 "2021-03-22T16:51:30Z")

</div>

Thanks.

I just finished tracing through my code and found a function called “constrainedValue”. In my case I pass 55.00 to “setValue”, but getValue returns 54.99…

Below is JUCE’s “setValue” implementation. “constrainedValue” literally changed the value I sent to it.

```
    void setValue (double newValue, NotificationType notification)
{
    // for a two-value style slider, you should use the setMinValue() and setMaxValue()
    // methods to set the two values.
    jassert (style != TwoValueHorizontal && style != TwoValueVertical);

    newValue = constrainedValue (newValue);

    if (style == ThreeValueHorizontal || style == ThreeValueVertical)
    {
        jassert (static_cast<double> (valueMin.getValue()) <= static_cast<double> (valueMax.getValue()));

        newValue = jlimit (static_cast<double> (valueMin.getValue()),
                           static_cast<double> (valueMax.getValue()),
                           newValue);
    }

    if (newValue != lastCurrentValue)
    {
        if (valueBox != nullptr)
            valueBox->hideEditor (true);

        lastCurrentValue = newValue;

        // (need to do this comparison because the Value will use equalsWithSameType to compare
        // the new and old values, so will generate unwanted change events if the type changes)
        if (currentValue != newValue)
            currentValue = newValue;

        updateText();
        owner.repaint();
        updatePopupDisplay (newValue);

        triggerChangeMessage (notification);
    }
}
```
