Skip to main content
1-Visitor
September 27, 2019
Solved

parseInt() not working for '08' and '09'. Why?

  • September 27, 2019
  • 6 replies
  • 2539 views

Hi,

 

Its really confusing, why parseInt('08') is giving NaN? 

parseInt('010') is 8?

parseInt('0101') is 65?

Is it working  on the basis of Binary?

but in JS it is working correctly.

 

 

Clarify Please,

 

 

Regards,

Janakiraman P

Best answer by smainente

You are correct, the JAR is named custom-rhino-js-5.1.0.jar in your version of ThingWorx.

It is apparently based on rhino 1.7R4 (as per MANIFEST.MF). This version is from 2012, it apparently complies to ECMAScript 3 + JavaScript 1.7 and some regex feature from JavaScript 1.8.

Regards,

Stéphane

6 replies

16-Pearl
September 27, 2019

Hi Janakiraman 

  • Javascript consider numbers that start with 0 to be octal
  • 08 and 09 are not valid octal numbers

See also https://stackoverflow.com/questions/850341/how-do-i-work-around-javascripts-parseint-octal-behavior

-- sma

 

1-Visitor
September 27, 2019

Hi,

I understood the problem.

 

in JavaScript it is working fine.

 

parseInt(string, radix);

 

If the input string begins with "0" (a zero), radix is assumed to be 8 (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 clarifies that 10 (decimal) should be used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.

 

But the problem has been resolved in ECMAScript 5.

 

Is Thingworx using ECMAScript 3 ?

 

 

 

Regards,

Janakiraman P

 

 

16-Pearl
September 27, 2019

Rhino is used as Javascript engine for ThingWorx Service.

To identify the version of Rhino used in your version of ThingWorx, look a the rhino JAR filename in <Tomcat>\webapps\Thingworx\WEB-INF\lib\

 

Regards.