I have a following code:

       InputField[Dynamic[coefA], Number, ImageSize-> 130]
       (*InputField[Dynamic[SetPrecision[coefA,3]], Number, ImageSize->130]*), 
       Slider[Dynamic[coefA], {-10., 10., .01}, ImageSize -> 130]
    {{coefA, 0}, ControlType -> None}, 
    AppearanceElements -> None]

I want both Slider and InputField to be able to modify coefA variable, however I would like to have a special formatting inside InputField - to 2 decimal points (like: 1.2358->1.24). The commented line works... for the Slider. It can modify variable and it is printed correctly, but I loose ability to modify it via InputField because:

"Tag SetPrecision in SetPrecision[-1.74,3] is Protected."

Any ideas how it can be done?

  • 1
    $\begingroup$ Is this enough? Dynamic[SetPrecision[coefA, 3], (coefA = #) &] $\endgroup$ – Kuba Dec 18 '15 at 12:22
  • $\begingroup$ This is closely related topic: 6259 but solutions from there are not working with Number type InputField. $\endgroup$ – Kuba Dec 18 '15 at 12:26
  • $\begingroup$ I've noticed that you haven't accepted any of answers to you questions. Are they missing something. Please, take a tour. $\endgroup$ – Kuba Jan 7 '16 at 9:56
  • $\begingroup$ Oh I'm sorry i totally forgot I asked this question here! You suggested the same solution as my teacher so i totally forgot to thank you - Thanks, @Kuba. Im gonna mark this as '[SOLVED]' now (how to do so...? :D) $\endgroup$ – michelson Jan 7 '16 at 14:39
  • $\begingroup$ No worries, I just wanted to tell you about the accept mark because sometimes users are not aware of it. $\endgroup$ – Kuba Jan 7 '16 at 14:41

So, you can use very general answer from Michael E2 (the bottom of his answer) or this quick fix:

    InputField[Dynamic[SetPrecision[coefA, 3], (coefA = #) &], Number], 
    Slider[Dynamic[coefA], {-10., 10., .01}]

Keep in mind that precision is not closely related to number of digits after decimal point.

To have 10.12 and 1.23 at once you have to use NumberForm[var, {Infinity, 2}] and approach from linked answer.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.