Google App Script: Passing value as integer instead of decimal

Viewed 673

I'm trying to create a post API payload from Google Script & the number field needs to be passed without decimal values. However, the app script always adds a decimal 0 (Eg. 1000.0) in the end which creates issues

var amt = '1000'; 
Logger.log(amt);
var options = {
    "method": "post",
      "payload": {
  "amount": parseInt(amt),

The logger replies with 1000, but when it is used in options, it passes 1000.0

Any help would be useful..

2 Answers

I had the same issue, solved it by the following

var amt = parseInt('1000').toFixed(0);
Logger.log(amt);
var options = {
    "method": "post",
      "payload": {
  "amount": amt,
}
     }
Logger.log(options)


The toFixed() allows you to fix the integer to any number of decimals you want.

You could pass the number in your post request as a string and then convert it to an integer on the receiving end

Sample:

Apps Script

var amt = '1000'; 
var options = {
    "method": "post",
      "payload": {
        "amount": amt,
       }
     }

Receiving end:

amt = parseInt(amt);
Related