javascript parseInt not converting to int value. Need to store data in to KVM as int values.

Not applicable

var outMsgId = parseInt(context.getVariable("out_Msg_Id"), 10) + 1;

The above statement is returning Float value. I tried Math utility and other parseInt methods. It is not working. Any Idea ?

Please share your ideas ASAP.

Solved Solved
1 1 2,078
1 ACCEPTED SOLUTION

This is a common question. In JavaScript, numbers are floating point values. Always.

If you want to get a representation of a number that looks like an integer, you can use the .toFixed() method. Like this:

$ jrunscript
nashorn> var msgId = "7";   
nashorn> var nextMsgId = (parseInt(msgId, 10) +1);
nashorn> nextMsgId
8.0
nashorn> nextMsgId = (parseInt(msgId, 10) +1).toFixed(0); 
8
nashorn> exit(0)

View solution in original post

1 REPLY 1

This is a common question. In JavaScript, numbers are floating point values. Always.

If you want to get a representation of a number that looks like an integer, you can use the .toFixed() method. Like this:

$ jrunscript
nashorn> var msgId = "7";   
nashorn> var nextMsgId = (parseInt(msgId, 10) +1);
nashorn> nextMsgId
8.0
nashorn> nextMsgId = (parseInt(msgId, 10) +1).toFixed(0); 
8
nashorn> exit(0)