Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

InputField[Dynamic[num],Number]

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

InputField[Dynamic[num],Expression]

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?

share|improve this question
    
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. –  Daniel W Mar 20 at 19:51
2  
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. –  Kuba Mar 20 at 20:02
    
how about input as String, then Read[StringToStream[],Number]] ? –  george2079 Mar 20 at 20:39

2 Answers 2

up vote 1 down vote accepted

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

 ClearAll["Global`*"];
 SetAttributes[sciInput, HoldFirst];
 inst[numstring_] := InputField[numstring, String, FieldSize -> {20, 1}, 
                   FieldHint -> "enter a number"];
 sciInput[result_] := 
       DynamicModule[{sig, nn}, 
          DynamicWrapper[Panel[Row[{inst[Dynamic@sig]}]], 
             Quiet[If[ (nn = Read[StringToStream[sig], Number]) === $Failed  , , 
                   If[NumberQ[nn], result = nn ]]], TrackedSymbols :> {sig}]];
 sciInput[number] // Deploy
 Dynamic[number]
share|improve this answer
    
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. –  Daniel W Mar 21 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]])

    sciInput[number]//Deploy

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}, 
      DynamicWrapper[
       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 
    Dynamic[number]
share|improve this answer
    
@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? –  Andy Mobley Mar 21 at 6:11
    
I'm glad you've made this answer :) Deploy will help you with Tab issue. –  Kuba Mar 21 at 7:08
    
@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. –  Andy Mobley Mar 21 at 8:54
    
Tab is working for me. Notice that the code in comment is composed of couple separated lines. Try to run it without If too. –  Kuba Mar 21 at 8:58
    
@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. –  Andy Mobley Mar 21 at 9:03

Your Answer

 
discard

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.