Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Script: Passing value as integer instead of decimal

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..

like image 551
Shanmuga Sundar Avatar asked Oct 28 '25 08:10

Shanmuga Sundar


2 Answers

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);
like image 142
ziganotschka Avatar answered Oct 30 '25 15:10

ziganotschka


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.

like image 28
kamiterru Avatar answered Oct 30 '25 13:10

kamiterru