Service Virtualization

  • 1.  How to handle big integer in JavaScript

    Posted May 10, 2017 07:39 AM

    Hi All,

     

    I need to handle big number using JavaScripts (70200000000000005355) but when am executing am getting the below error.

    long Number = 70200000000000005355;

    Can you please help.

     

    ============================================================================
    | Error in Script
    ============================================================================
    | Step: Write to an Excel
    ----------------------------------------------------------------------------
    | Message: Parse error at line 19, column 15 : Number too big for integer type: 70200000000000005355
    ----------------------------------------------------------------------------
    | Trapped Exception: Parse error at line 19, column 15 : Number too big for integer type: 70200000000000005355
    | Trapped Message: bsh.ParseException: Parse error at line 19, column 15 : Number too big for integer type: 70200000000000005355
    ----------------------------------------------------------------------------
    STACK TRACE
    bsh.ParseException: Parse error at line 19, column 15 : Number too big for integer type: 70200000000000005355
    at bsh.Parser.createParseException(Parser.java:129)
    at bsh.Parser.Literal(Parser.java:2307)
    at bsh.Parser.PrimaryPrefix(Parser.java:2171)
    at bsh.Parser.PrimaryExpression(Parser.java:2093)
    at bsh.Parser.PostfixExpression(Parser.java:2030)
    at bsh.Parser.UnaryExpressionNotPlusMinus(Parser.java:1913)
    at bsh.Parser.UnaryExpression(Parser.java:1805)
    at bsh.Parser.MultiplicativeExpression(Parser.java:1699)
    at bsh.Parser.AdditiveExpression(Parser.java:1656)
    at bsh.Parser.ShiftExpression(Parser.java:1597)
    at bsh.Parser.RelationalExpression(Parser.java:1530)
    at bsh.Parser.InstanceOfExpression(Parser.java:1502)
    at bsh.Parser.EqualityExpression(Parser.java:1459)
    at bsh.Parser.AndExpression(Parser.java:1416)
    at bsh.Parser.ExclusiveOrExpression(Parser.java:1384)
    at bsh.Parser.InclusiveOrExpression(Parser.java:1341)
    at bsh.Parser.ConditionalAndExpression(Parser.java:1298)
    at bsh.Parser.ConditionalOrExpression(Parser.java:1255)
    at bsh.Parser.ConditionalExpression(Parser.java:1215)
    at bsh.Parser.Expression(Parser.java:1115)
    at bsh.Parser.VariableInitializer(Parser.java:624)
    at bsh.Parser.VariableDeclarator(Parser.java:552)
    at bsh.Parser.TypedVariableDeclaration(Parser.java:3359)
    at bsh.Parser.BlockStatement(Parser.java:2809)
    at bsh.Parser.Line(Parser.java:200)
    at bsh.Interpreter.Line(Interpreter.java:1014)
    at bsh.Interpreter.eval(Interpreter.java:651)
    at bsh.Interpreter.eval(Interpreter.java:758)
    at bsh.Interpreter.eval(Interpreter.java:747)
    at com.itko.lisa.test.ScriptExecHandler.executeScript(ScriptExecHandler.java:636)
    at com.itko.lisa.test.ScriptExecHandler.executeScriptOld(ScriptExecHandler.java:528)
    at com.itko.lisa.test.ScriptNode._execute(ScriptNode.java:126)
    at com.itko.lisa.editor.ScriptNodeEditor.execute(ScriptNodeEditor.java:100)
    at com.itko.lisa.editor.ScriptNodeEditor.access$000(ScriptNodeEditor.java:42)
    at com.itko.lisa.editor.ScriptNodeEditor$1.actionPerformed(ScriptNodeEditor.java:61)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Caused by: java.lang.NumberFormatException: Number too big for integer type: 70200000000000005355
    at bsh.Parser.parseInt(Parser.java:150)
    at bsh.Parser.Literal(Parser.java:2305)
    ... 69 more
    ============================================================================

     

    Thanks

    Ram



  • 2.  Re: How to handle big integer in JavaScript
    Best Answer

    Posted May 10, 2017 09:36 AM

    This issue nothing to do with Devtest. This issue due to exceeding the length for int data type.  

     

    70200000000000005355 your value > int.MAX_VALUE
    You will need either BigInteger or BigDecimal or long Integer. Select the one suitable for your requirement. 

     

    Hope this helps. 

     

    Thanks, 

    Rajesh k Singh



  • 3.  Re: How to handle big integer in JavaScript

    Posted May 10, 2017 01:02 PM

    Hi Rajesh,

     

    i have tried the above suggested data types declaration, but still getting the same error. Please help

     

    Thanks

    Ram



  • 4.  Re: How to handle big integer in JavaScript

    Broadcom Employee
    Posted May 10, 2017 01:15 PM

    Java recognises it as an int as it tries to allocate it as a long. The standard way in Java to do this is to add "L" to the end of the number, forcing Java to see it as a long.

    long Number = 70200000000000005355L;



  • 5.  Re: How to handle big integer in JavaScript

    Posted May 10, 2017 02:13 PM

    Hi Rick,

     

    i have tried by adding L at the end and getting the below error

     

    Target exception: java.lang.ClassCastException: Cannot cast java.lang.String to long

     

    Thanks

    Ram



  • 6.  Re: How to handle big integer in JavaScript

    Broadcom Employee
    Posted May 10, 2017 02:39 PM

    From that error, I presume you mistakenly put double-quotes around it

    ?



  • 7.  Re: How to handle big integer in JavaScript

    Posted May 12, 2017 02:45 AM

    Thanks Rick...