'Google Apps Script JSON element to Float

I'm using the following script on Google Apps Script to retrieve the value of my Helium wallet:

function getBalance() {
  try {
    var url = 'https://api.helium.io/v1/accounts/1451THihiR9hDzkgdn4ZMSTzC8cuQJh9StXJGVPEcTPEXjKzjdH'
    var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
    var data = JSON.parse(response.getContentText());
    var balance = data.data.balance;
    var myTime = new Date();
    Logger.log(balance);
    Logger.log(myTime);
  } catch (err) {
    Logger.log(data)
    Logger.log(err.message);
  }
}

My issue is that the number that is returned as balance is:

9:26:42 PM  Info  5.08178108E8

The balance is actually 5.08178108. I want to convert the element into a Float but I can't find a way to do that. Converting it to a string removes the decimal. How do I convert the balance into a Float while retaining the decimal position?



Solution 1:[1]

Thanks to tanaike for this answer:

I ended up converting the balance into a Float by dividing by 1E8. Here's the entire function:

function getBalance() {
  try {
    var url = 'https://api.helium.io/v1/accounts/1451THihiR9hDzkgdn4ZMSTzC8cuQJh9StXJGVPEcTPEXjKzjdH'
    var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
    var data = JSON.parse(response.getContentText());
    var balance = data.data.balance / 1E8;
    var myTime = new Date();
    Logger.log(balance);
    Logger.log(myTime);
  } catch (err) {
    Logger.log(data)
    Logger.log(err.message);
  }
}

This allowed me to set the value in my Google Sheet and make calculations off of it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user3063045