Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
Related: Rounding problems inside InputField – Szabolcs May 31 '12 at 8:35
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. – István Zachar May 31 '12 at 9:46
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? – Szabolcs May 31 '12 at 10:02
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. – nilo de roock May 31 '12 at 10:03
@Szabolcs to .123 and display as .12, And yes. – nilo de roock May 31 '12 at 10:03
up vote 2 down vote accepted

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]
share|improve this answer
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. ) – nilo de roock May 31 '12 at 9:57
@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. – image_doctor May 31 '12 at 12:00
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. – nilo de roock May 31 '12 at 12:18
The \ sign shows if the field is cut off, which is good. – nilo de roock May 31 '12 at 12:31
@ndroock1 I'm pleased that the characteristics help in your context :) – image_doctor May 31 '12 at 14:06

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;

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

and so on.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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