How to set precision and format labeled Slider in Manipulate?

Two, now three, ways: I think I'd recommend the first -- strike that -- maybe the third one at the end, but perhaps you have a reason for using SetPrecision, in which case, use the second.

This first one works as is. One caveat: if the label is used as an input field, the number k will be set to a machine-precision number.

Manipulate[
 Plot[Cos[k x], {x, 0, 2 Pi}],
 {{k, 1}, 0.`22, 4.`22, 0.1`22, Appearance -> "Labeled"}]

The second way is to make your own label. Here editing the label still maintains the desired precision.

Manipulate[
 Plot[Cos[k x], {x, 0, 2 Pi}],
 {{k, 1}, SetPrecision[0., 22], SetPrecision[4.0, 22], SetPrecision[0.100, 22], 
  Row[{Manipulator[##], "  ", 
     InputField[Dynamic[Round[k, 0.1], (k = SetPrecision[#, 22]) &], 
      Appearance -> "Frameless", BaseStyle -> "Label"]}] &}
 ]

Here is a variation with a non-editable label:

Manipulate[
 Plot[Cos[k x], {x, 0, 2 Pi}],
 {{k, 1}, SetPrecision[0., 22], SetPrecision[4.0, 22], SetPrecision[0.100, 22],
  Row[{Manipulator[##], "  ", Dynamic@Round[k, 0.1]}] &}]

Both look the same.

Mathematica graphics


There is a difference between setting the precision with SetPrecision and with a backtick:

x = SetPrecision[0.1, 22]
(* 0.1000000000000000055511 *)

y = 0.1`22
(* 0.1000000000000000000000 *)

Precision /@ {x, y}
(* {22., 22.} *)

x - y
(* 5.5511*10^-18 *)

Alternatively, you can use a dummy variable and convert it to the desired precision:

Manipulate[
 k = N[Round[k0, 1/10], 22];
 Plot[Cos[k x], {x, 0, 2 Pi}, PlotLabel -> k],
 {{k0, 1, "k"}, 0, 4, 0.1, Appearance -> "Labeled"},
 {k, None},
 TrackedSymbols :> {k0}]

(I included a plot label to verify that k has the proper precision.)


Because I'm new to Mathematica, I thought it may be helpful to newer people to answer this question the way a "new user" may think about the problem without the use of the more compact symbols. Obviously, MichaelE2 has covered the gambit of options already, and the solution below is negligibly different from his "non-editable label" solution.

To create the desired result, I used a Row[] construct that has the control's label k, the slider, some added space for aesthetic purposes, and the variable displayed as a numerical approximation all next to each other.

Manipulate[Plot[Cos[k x], {x, 0, 2 Pi}],
  Row[{Control[{{k, 1}, 0, SetPrecision[4.0, 22], 
    SetPrecision[0.100, 22]}], Spacer[5], Dynamic[N[k, 2]]}]
]

enter image description here

Whenever a control is placed in a row, it must be identified by wrapping it with the Control[] function. The numerical approximation must be wrapped in a Dynamic[] because it should be updated whenever the slider is moved. If you wanted to change the number of digits that was displayed, all you have to do is change the number in the second argument of N[].


You mean something like this?

Manipulate[
 Plot[Cos[k x], {x, 0, 2 Pi}, Frame -> True, Axes -> False, ImagePadding -> 30],

 Text[Style[

   Grid[{ {Style["k", Italic], Spacer[2],
      Manipulator[Dynamic[k, {k = SetPrecision[#, 22]} &], {0, to, del}, 
       ImageSize -> Tiny],

      Spacer[2],

      Dynamic[AccountingForm[k , (*choose how many digits to show*){23, 22}, NumberSigns -> {"", ""}, 
        NumberPadding -> {"0", "0"}, SignPadding -> True]]}},

     Spacings -> {0.1, .1}, Frame -> True, FrameStyle -> Directive[Thickness[.001], Gray]
    ],
   20]
  ],
 {{k, 1}, None},
 Initialization :>
  (
   to = SetPrecision[4.0, 22];
   del = SetPrecision[0.100, 22]
   )
 ]

enter image description here