String to Integer Method Problem

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?

What is the output of vBoxNumStr ?

Starting with the string “WK1607-01”, after repeated clicks, I get the following output of vBoxNumStr and vBoxNum from lines 3 and 4 respectively:

01    1.0
02    2.0
03    3.0
04    4.0
05    5.0
06    6.0
07    7.0
08    NaN
08    NaN

Version 3.1.4-build 408
Java version 1.6.0_01-b06 (Windows XP)

What happens when you do this with a ‘clean’ string. So not extracted from a larger string but simply ‘01’, ‘02’ etc.

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…

When I try it with a “clean” string (ie starting from “01”, no prefix), exactly the same happens when it gets to 08, I get NaN.

Very strange indeed…

As Patrick said NaN means that it is not a number.

What happens when you do vBoxNumStr * 1 instead of the parseInt()?

IT2Be:
What happens when you do vBoxNumStr * 1 instead of the parseInt()?

That works OK. I’ll go with that, thanks Marcel. Just find it strange that parseInt behaves that way…

So do I!
But, to be honest, I hardly ever cast that way.
I always do it as suggested…

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

utils.stringToNumber('08');

That works with ‘08’…

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,

http://www.devguru.com/Technologies/ecm … seint.html

The result from “parseInt(‘08’)” in Firefox is “0” – which is a bit more understandable.

Servoy won’t except the standard “radix” argument to specify the number system to use, which would be nice.

greg.

Look at the definition of parseint (from http://www.w3schools.com/jsref/jsref_parseInt.asp):

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.

Rob