Skip to main content
Known Participant
April 18, 2014
Answered

Convert GUI "edittext" to a Number

  • April 18, 2014
  • 2 replies
  • 1837 views

Hi There

I´ve build a GUI with an edittext box where I want to put in numbers, to use them later for calculating, generating layers and so on.

But I always come along with NaN (Not a Number).

Script example:

var myWin = new Window("palette","Create a number of layers in a circle",undefined);

myWin.orientation = "column";

 

var groupOne = myWin.add("group", undefined, "GroupOne");

groupOne.orientation = "row";

groupOne.add("statictext", undefined, "Number of Pieces");

var inputNumber = groupOne.add("edittext", [0,0,40,20], "6");

How can I convert this edit text (6) from that string to a number, so that I can use it?

And, have I to check,  that my input text is a number?

THX for your help ....

This topic has been closed for replies.
Correct answer Paul Tuersley

It would be best to check and store the value when the user changes it rather than waiting until some "Start" button has been pressed, so here's an example where you set an onChange event for the EditText which calls a function.

The control that calls the function can be accessed using 'this'. Also, if using parseInt to convert to whole numbers, be sure to specify the radix (number base system) as shown to avoid unpredictable results. Also you'll need to use the built in isNaN() method to check if the result is Not a Number.

var myGlobalNumberVariable = 1;

inputNumber.text = myGlobalNumberVariable;

inputNumber.onChange = onInputNumberChange;

function onInputNumberChange() {

 

          var result = parseInt(this.text, 10);

          //var result = parseFloat(this.text);

 

          // use parseFloat to keep decimals, parseInt for whole numbers.

          // note you should specify the radix for parseInt to ensure it converts to base 10 (decimal).

          // for example, parseInt("012") will assume it's a base 8 value and return 10 (any value with leading zero is assumed to be base 8 if no radix is specified.

  if (isNaN(result) || result < 1 || result > 100) {          // if not a number or outside a range you might choose

    this.text = myGlobalNumberVariable;          // revert back to the previous setting.

  } else {

    myGlobalNumberVariable = result;

    this.text = result; // set the text box to the result of the parseInt which may be different, for example it will strip any leading zeros or decimals.

  }

}

2 replies

Paul TuersleyCorrect answer
Inspiring
April 18, 2014

It would be best to check and store the value when the user changes it rather than waiting until some "Start" button has been pressed, so here's an example where you set an onChange event for the EditText which calls a function.

The control that calls the function can be accessed using 'this'. Also, if using parseInt to convert to whole numbers, be sure to specify the radix (number base system) as shown to avoid unpredictable results. Also you'll need to use the built in isNaN() method to check if the result is Not a Number.

var myGlobalNumberVariable = 1;

inputNumber.text = myGlobalNumberVariable;

inputNumber.onChange = onInputNumberChange;

function onInputNumberChange() {

 

          var result = parseInt(this.text, 10);

          //var result = parseFloat(this.text);

 

          // use parseFloat to keep decimals, parseInt for whole numbers.

          // note you should specify the radix for parseInt to ensure it converts to base 10 (decimal).

          // for example, parseInt("012") will assume it's a base 8 value and return 10 (any value with leading zero is assumed to be base 8 if no radix is specified.

  if (isNaN(result) || result < 1 || result > 100) {          // if not a number or outside a range you might choose

    this.text = myGlobalNumberVariable;          // revert back to the previous setting.

  } else {

    myGlobalNumberVariable = result;

    this.text = result; // set the text box to the result of the parseInt which may be different, for example it will strip any leading zeros or decimals.

  }

}

Legend
April 18, 2014

I think you can use javascript's basic parseInt() or parseFloat() methods.  So you would do something like:

var myNumber = parseInt(inputNumber.value);

I think it returns null or some other falsey value if it cannot successfully parse the string into an integer.  If you are expecting the user to enter in a floating point number, then use parseFloat instead.

--Arie

ab_VFXAuthor
Known Participant
April 19, 2014

Thank you Arie, but your code dosn´t work in a way I need.

The code of Paul work fine. Thanks for these lines.

--Andreas