2
$\begingroup$

Background: I want all numbers in InputField's displayed as NumberForm[num,2];

Now, consider the following the snippet of code:

 latsy = 1.1;
 latparshx = 32;
 piet = NumberForm[latsy*Tan[latparshx*Pi/180], 2];
 InputField[Dynamic[piet], FieldSize -> 3]

This code produces an inputfield with a number between double quotes, which is not what I expected. I basically want 0.69 displayed.

Question: How can I ( what is the best way to ) control the display format of a number in an InputField ?

EDIT: The InputField resizes itself on the Panel it is positioned on and thus messes up the other GUI elements.

$\endgroup$
8
  • 2
    $\begingroup$ Related: Rounding problems inside InputField $\endgroup$
    – Szabolcs
    May 31, 2012 at 8:35
  • $\begingroup$ Maybe you could define a new style with options like PrintPrecision -> 2, and then use InputField[piet, Number, BaseStyle -> {"MyStyle"}] (removing the NumberForm wrapper from piet). This is the most I could dig up from the Documentation, and since I don't know how to define styles from a notebook, I'm stuck here. $\endgroup$ May 31, 2012 at 9:46
  • $\begingroup$ What would happen if I enter 0.123? Would the variable be set to 0.12 or 0.123? If it is set to 0.123 and displayed as 0.12 in the InputField, but then I click in the InputField again and press enter, will that change the value to 0.12? $\endgroup$
    – Szabolcs
    May 31, 2012 at 10:02
  • $\begingroup$ Me too, but I can dig into it. I am not keen on changing Notebook options though. I am surprised that this turns out to be so awkward. $\endgroup$ May 31, 2012 at 10:03
  • $\begingroup$ @Szabolcs to .123 and display as .12, And yes. $\endgroup$ May 31, 2012 at 10:03

2 Answers 2

2
$\begingroup$

You could use Round to control the representation.

latsy = 1.1;
latparshx = 32;
piet = Round[latsy*Tan[latparshx*Pi/180], 0.01];
InputField[Dynamic[piet], FieldSize -> 3]

EDIT: If rounding is desirable limiting the size of the input field is an option

latsy = 1.1;
latparshx = 32;
piet = latsy*Tan[latparshx*Pi/180];
InputField[Dynamic[piet], Number, FieldSize -> {3, 1},ContinuousAction -> True]
$\endgroup$
5
  • $\begingroup$ Do you mean that I should use two variables, one for presentation and one for further calculations? Is that how it is done? How you do it? ( Rounding the data in the app I work on is not acceptable, would lose too much precision. ) $\endgroup$ May 31, 2012 at 9:57
  • $\begingroup$ @ndroock1 Two variables could be a possibility, but seems unpleasant as a solution. I've added another option which limits the displayed size of the input field. But it just masks the remaining digits of the number, which is confusing if you intend users to edit it. $\endgroup$ May 31, 2012 at 12:00
  • $\begingroup$ The 'editing' is done via controls mostly. The inputfield is a last resort fine-tuning option, so yes it's confusing but better than maintaining two sets of vars in the program. I'll see how it works. $\endgroup$ May 31, 2012 at 12:18
  • $\begingroup$ The \ sign shows if the field is cut off, which is good. $\endgroup$ May 31, 2012 at 12:31
  • $\begingroup$ @ndroock1 I'm pleased that the characteristics help in your context :) $\endgroup$ May 31, 2012 at 14:06
2
$\begingroup$

If all you want to do is have your stuff displayed without the string characters then just do this -- i.e. keeping the code you have already:

latsy = 1.1;
latparshx = 32;
piet = Style[NumberForm[latsy*Tan[latparshx*Pi/180], 2], 
   ShowStringCharacters -> False
   ];

InputField[Dynamic[piet], FieldSize -> 3]

But there are other things you could also do, e.g. István Zachar mentioned PrintPrecision:

latsy = 1.1;
latparshx = 32;
piet = 0.12345678;

Style[
 InputField[Dynamic[piet], FieldSize -> 3],
 PrintPrecision -> 2
 ]

and so on.

$\endgroup$

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.