# How can I prevent precision of value displayed by a labeled slider from changing?

I'm experiencing a small glitch on the label of a slider. When I change the value of another slider, the time slider may display its value at higher precision (5 digits after the decimal point), while it normally shows only two. I want to keep it at 2 digits after the decimal point. How can I do that?

Here's a minimal working example to show the problem :

Manipulate[
If[T > Sqrt[a], T = Sqrt[a]];
Plot[Sin[2 Pi a (t - T)], {t, -10, 10},
Frame -> True, PlotRange -> {{-5, 5}, {-1, 1}}],
{{a, 1, Style["Frequency", 10]}, 0, 5, 0.01, Appearance -> {"Labeled"}},
{{T, 0, Style["Time translation", 10]}, -10, Dynamic[Sqrt[a]], 0.01,
Appearance -> {"Labeled"}}]


Just set the two sliders to their max value, then lower the first parameter ("Frequency"). You'll see the second label with too many digits (five digits after the decimal point). I want that label to always shows 2 digits only.

So how can I constrain the label output to 2 digits only, after the decimal point, in all circumstances?

• Does NumberForm not work? – Michael E2 Dec 22 '16 at 18:34
• @MichaelE2, apparently not. The problem is the updated value of the "Time" label, which has too much digits. I don't see where to change it. Is this a limitation of the manipulate implementation of Mathematica ? – Cham Dec 22 '16 at 18:41

The problem arises when your code adjusts the value of T to keep it below Sqrt[a]. When that happens and the T slider is also at its maximum, the value of T shown to right of time translation control must be updated immediately. This update seems to bypass the normal mechanism used update the control value. And once the value gets corrupted, it stays corrupted until the time translation control is manipulated by the user.

This problem can be fixed coercing T to only take on values that have the right granularity. You use the granularity .01, but I have changed it to .1 in this answer, because the lower value makes it easier to see what is going on.

Manipulate[
If[T > Sqrt[a], T = Floor[Sqrt[a], .1]];
Plot[Sin[2 Pi a (t - T)], {t, -10, 10},
Frame -> True,
PlotRange -> {{-5, 5}, {-1, 1}}],
{{a, 1, "Frequency"}, 0, 5, .1, Appearance -> "Labeled"},
{{T, 0, "Time translation"}, -10, Dynamic[Floor[Sqrt[a], .1]], .1,
Appearance -> "Labeled"}]

• It works, but it's not obvious to me how to adapt this to my full code. – Cham Dec 23 '16 at 2:16
• When I use your trick in my full code, changing some parameters may make MMA to complain : Input value {... } lies outside the range of data in the interpolating function. Extrapolation will be used. – Cham Dec 23 '16 at 2:22
• @Cham. I can only work with the code you post. – m_goldberg Dec 23 '16 at 2:22
• I understand, but I can't post my full code ! It's too long and complicated just for this precision problem displayed in the label. – Cham Dec 23 '16 at 2:23
• @Cham You should bound the range of the slider so that the input into the interpolating function stays within its domain. If there's a complicated relationship between the parameters and the input, then it might be a hard problem to solve. You could always use FindRoot inside Dynamic to set the limit on T. OR, use Quiet, if the extrapolation error is negligible. – Michael E2 Dec 24 '16 at 17:25