VMware Cloud Community
Windspirit
Hot Shot
Hot Shot

Major problems with parseInt in vRO

parseInt seems to have massive problems with 8 and 9 when you add leading ZEROS to it.

exampel script:

for (i=0;i<10;i++){

    strNumber=i.toString();

    System.log(strNumber);

    System.log(parseInt("0"+strNumber));

    System.log(parseInt("00"+strNumber));

    System.log(parseInt("000"+strNumber));

    System.log(parseInt("0000"+strNumber));

}

output:

[2017-06-01 10:01:10.222] [I] 0

[2017-06-01 10:01:10.223] [I] 0

[2017-06-01 10:01:10.224] [I] 0

[2017-06-01 10:01:10.225] [I] 0

[2017-06-01 10:01:10.226] [I] 0

[2017-06-01 10:01:10.227] [I] 1

[2017-06-01 10:01:10.228] [I] 1

[2017-06-01 10:01:10.229] [I] 1

[2017-06-01 10:01:10.230] [I] 1

[2017-06-01 10:01:10.231] [I] 1

...

[2017-06-01 10:01:10.257] [I] 7

[2017-06-01 10:01:10.258] [I] 7

[2017-06-01 10:01:10.259] [I] 7

[2017-06-01 10:01:10.260] [I] 7

[2017-06-01 10:01:10.261] [I] 7

[2017-06-01 10:01:10.262] [I] 8

[2017-06-01 10:01:10.263] [I] NaN

[2017-06-01 10:01:10.264] [I] 0

[2017-06-01 10:01:10.265] [I] 0

[2017-06-01 10:01:10.266] [I] 0

[2017-06-01 10:01:10.267] [I] 9

[2017-06-01 10:01:10.268] [I] NaN

[2017-06-01 10:01:10.269] [I] 0

[2017-06-01 10:01:10.270] [I] 0

[2017-06-01 10:01:10.271] [I] 0

What one has to do is:

parseInt(nun,10);

The ,10 is an old depricated feature of parseInt that lets you choose your BASE...and we want the 10 base not ocatal (8 = default when leading 0)....thats why it fails at 8 and 9.

wonderFOOL

for (i=0;i<10;i++){

    strNumber=i.toString();

    System.log(strNumber);

    System.log(parseInt("0"+strNumber,10));

    System.log(parseInt("00"+strNumber,10));

    System.log(parseInt("000"+strNumber,10));

    System.log(parseInt("0000"+strNumber,10));

}

0 Kudos
2 Replies
iiliev
VMware Employee
VMware Employee

Hi Daniel,

Actually, Mozilla's Javascript reference documentation - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt - explicitly recommends radix parameter to be always specified when using parseInt() to guarantee predictable behavior. So I don't see this as a major problem with vRO and Rhino JS engine.

0 Kudos
Windspirit
Hot Shot
Hot Shot

Its now fixed in vRO 7.3

parseint(string) is no base 10 by default.

0 Kudos