I am trying to make an InputField for numeric input, but


Does not allow the user to type 1.3*^-13 or 1.3*10^-13, for example. The characters are simply disallowed in the field. I tried


which allows 1.3*10^-13 but does not allow the 1.3*^-13 shorthand. The *^ simply disappears when I type the -. Further exploration shows that it the shorthand entry does work with positive exponents like 1.3*^6.

So, my workaround is to use Expression and to remember to use explicit scientific notation whenever I have a negative exponent, but allowing an Expression in an InputField is always a dangerous proposition. Is there any way to get this to work with Number?

  • $\begingroup$ I know this is kind of a weak question since I have already found a workaround, but I wanted to get this behavior documented here as it seems buggy to me. $\endgroup$ – Daniel W Mar 20 '14 at 19:51
  • 2
    $\begingroup$ It has further consequences. In free CDFs you can only use Number type InputField. For this case I'd use two InputFields, one for base and second for exponent and something between like "*10^" so a user will know what to do. $\endgroup$ – Kuba Mar 20 '14 at 20:02
  • $\begingroup$ how about input as String, then Read[StringToStream[],Number]] ? $\endgroup$ – george2079 Mar 20 '14 at 20:39

This accepts a plain number or "E" format or "*^" notation..

 SetAttributes[sciInput, HoldFirst];
 inst[numstring_] := InputField[numstring, String, FieldSize -> {20, 1}, 
                   FieldHint -> "enter a number"];
 sciInput[result_] := 
       DynamicModule[{sig, nn}, 
             Quiet[If[ (nn = Read[StringToStream[sig], Number]) === $Failed  , , 
                   If[NumberQ[nn], result = nn ]]], TrackedSymbols :> {sig}]];
 sciInput[number] // Deploy
  • $\begingroup$ Thanks to everybody for their help. I am not quite ready to attack creating my own control type, so I will study these approaches carefully. I selected this answer because it gives the option of entering the number in normal notation as well as scientific. $\endgroup$ – Daniel W Mar 21 '14 at 19:20

This Answer is based upon Kuba's comment above regarding two InputFields, so credit should go to him for the idea, this is just an implementation. However, the problem interested me because I have a similar issue to solve regarding entering units into answers, and this question and Kuba's comment made me think of a related solution to that.

One can use Interpretation to make a relatively clean looking input that will "guide" the user through the input.

    numbIF[num_] := InputField[Dynamic[num], Number, FieldSize -> Full];
    expIF[exp_] := InputField[Dynamic[exp], Number, FieldSize -> Full];

    SetAttributes[sciInput, HoldFirst];
    sciInput[result_] := (Panel[
       Interpretation[{significand, exponent}, 
        Row[{numbIF[significand], Style[" x", 12], Superscript[10, expIF[exponent]]}], 
        result = significand*10^exponent]])


Mathematica graphics

The ideas for setting up the Interpretation came from the Wolfram GeneralizedInput Tutorial. Kuba pointed out including //Deploy to allowing tabbing between fields.

Kuba provided this nice alternative to the Interpretation approach. (Thanks)

    ClearAll["Global`*"]; SetAttributes[sciInput, HoldFirst]; 
    IF[num_] := InputField[num, Number, FieldSize -> Full]; 
    sciInput[result_] := DynamicModule[{sig, exp}, 
       Panel[Row[{IF[Dynamic@sig], Superscript[10, IF[Dynamic@exp]]}, Style[" x", 12]]], 
       If[And @@ (NumericQ /@ {sig, exp}), result = sig 10^exp], 
       TrackedSymbols :> {sig, exp}]] ;

    sciInput[number] // Deploy 
  • $\begingroup$ @Kuba. Your comment led me to this answer. Please let me know if you feel like I stepped on your toes (really don't want to do that as newbie on the site). I couldn't figure out how to be able to tab from InputField to InputField which would really make the solution much nicer. Any ideas? $\endgroup$ – Andy Mobley Mar 21 '14 at 6:11
  • $\begingroup$ I'm glad you've made this answer :) Deploy will help you with Tab issue. $\endgroup$ – Kuba Mar 21 '14 at 7:08
  • $\begingroup$ @Kuba I like your addition. I don't think you need the NumericQ checking since you have defined the InputField to be Number. In your example I still don't get the tab to advance between fields, but I will go back and look at the link you provided for additional clues. I haven't worked with Deploy before, so I will need to do some learning. $\endgroup$ – Andy Mobley Mar 21 '14 at 8:54
  • $\begingroup$ Tab is working for me. Notice that the code in comment is composed of couple separated lines. Try to run it without If too. $\endgroup$ – Kuba Mar 21 '14 at 8:58
  • $\begingroup$ @Kuba Yes, the tabbing worked just fine when I added to my Interpretation code. Thanks again for your support as I get my feet wet with Mathematica.SE. $\endgroup$ – Andy Mobley Mar 21 '14 at 9:03

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.