Skip to main content
Participating Frequently
November 25, 2016
Answered

Convert text value to numeric value

  • November 25, 2016
  • 5 replies
  • 1349 views

Hi all,

Is there a way to convert a text value of one text field to a numeric value in another text field? For example, if text field's "MonthText" value is "January", then the text field's "MonthNum" value is "01". I'm sure there is a way, but my knowledge is limited...

Thanks

This topic has been closed for replies.
Correct answer try67

Yes, that's correct. So to tie them you need to get the index of the value in the first array and then use that some index in the second one.

This could be the custom calculation script of the "MonthNum" field:

var monthTexts = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

var monthNumbers = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];

var monthText = this.getField("MonthText").valueAsString;

var i = monthTexts.indexOf(monthText);

if (i==-1) event.value = "";

else event.value = monthNumbers;

In this case, though, since the second array is just numbers then you don't actually need it. You can use this instead (as the last line of the code):

else event.value = util.printf("%02d", (i+1));

5 replies

try67
Community Expert
Community Expert
November 25, 2016

There are many ways this can be done... For example, you can use an array of literal objects, or two arrays, one with the name of the month and one with its number, etc.

Participating Frequently
November 28, 2016

Thanks for your reply. Like I said, I don't have much experience with this, but as far as I understand, it would go something like this?:

var MonthText = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November",                                          "December"];

var MonthNum = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];

Now, how do I tie them? If.... ??

Legend
November 28, 2016

You'd step through the MonthText array looking at each value. If you find a match at a given index, take the item with the same index from MonthNum.