Javascript converting the number to exponential form while using context.setVariable

Hi @dchiesa1  ,

I have created a mock in Apigee which should send back a random serial number of length 8.

Code:

 

context.setVariable("serialNumber",(Math.floor(Math.random() * 90000000)));

 

Output(Number in exponential format):

 

"serialNumber": "3.7680179526860476E7",

 

Output I need: 37680179

Can you please help?

Solved Solved
0 2 189
1 ACCEPTED SOLUTION

Hi @Rohit_Kancharla 

You can get want you want simply by converting to string.

context.setVariable("serialNumber",(Math.floor(Math.random() * 90000000)).toString());

And as a bonus, you can inline using the Source element

<Javascript continueOnError="false" enabled="true" timeLimit="200" name="JS-random">
  <Source>context.setVariable("serialNumber",(Math.floor(Math.random() * 90000000)).toString());</Source>
</Javascript>

 

View solution in original post

2 REPLIES 2

Hi @Rohit_Kancharla 

You can get want you want simply by converting to string.

context.setVariable("serialNumber",(Math.floor(Math.random() * 90000000)).toString());

And as a bonus, you can inline using the Source element

<Javascript continueOnError="false" enabled="true" timeLimit="200" name="JS-random">
  <Source>context.setVariable("serialNumber",(Math.floor(Math.random() * 90000000)).toString());</Source>
</Javascript>

 

Thank you, @kurtkanaskie!