I have a field, called “box_ref”, format string, which contains a reference number in the following format:
WKwwyy-XX where ww is the week of the year, yy is the year and XX is an incremental number. Example: WK1507-01 would be box number 1 of week 15 of 2007.
I have written the following method that is attached to 2 button that increment or decrement the end number of that ref, thus:
var vBtnName = application.getMethodTriggerElementName();
if(!box_ref){ return; }//if no box ref, do nothing
var vBoxNumStr = box_ref.substr(7,2); //get the box ref string
var vBoxNum = parseInt(vBoxNumStr); //get last 2 numbers as int
if(vBtnName == 'btn_ref_up' & vBoxNum <100){ //increment
vBoxNum ++;
}
else if(vBtnName == 'btn_ref_dn' & vBoxNum >1) { //decrement
vBoxNum --;
}
else { return; }
vBoxNum = "" + vBoxNum;//turn vBoxNum into a string
if(vBoxNum.length <2){ vBoxNum = "0" + vBoxNum; } //if length is less than 2, add 0 to front
box_ref = utils.stringMiddle(box_ref,1,7) + vBoxNum; //create full string
controller.saveData();
The problem is that every time the last two digits equal 08 or 09 the number will not decremet or increment. Other numbers (smaller and larger) are fine except these two.
When I debug the debugger says vBoxNum is NaN (not a number) when it gets past line 4.
Any ideas what’s going on? Why is it just 08 and 09 that dont play ball?
Could you make an output that also shows the original String where you take the number out? I can tell you only one thing: NaN means Not A Number, so something goes wrong when creating the number…
Very strange, indeed. I am using parseInt() and never had a problem. But I tested it and can confirm that parseInt(‘08’) returns NaN. What you can also do is
It does seem that parseInt is a bit non-standard javascript in Servoy. But, it also doesn’t behave the way you would expect in standard javascript interpreters. See,
If the radix parameter is omitted, JavaScript assumes the following:
* If the string begins with "0x", the radix is 16 (hexadecimal)
* If the string begins with "0", the radix is 8 (octal). This feature is deprecated
* If the string begins with any other value, the radix is 10 (decimal)
08 is interpreted as octal string, but 8 is not a valid octal digit.
You should supply the radix argument.