Ad

How Can I Set The Number Of Decimals On A Popup Using JavaScript?

- 1 answer

I have a popup on a website that displays values from a geoJSON file. Right now, it looks as in the picture below: enter image description here

This is the current code:

function popUp_cc_210303(f, l) {
  var out = [];
  if (f.properties) {
    var url =
      '<a href=/uas_tools/crop_analysis/index.php?crop=Wheat&year=2021&location=Amarillo&sublocation=Irrigation';
    var parameters = '';
    for (key in f.properties) {
      out.push(key + ': ' + f.properties[key]);
      parameters += '&' + key.replace(/\ /g, '_') + '=' + f.properties[key];
    }
    url +=
      parameters.replace(/\ /g, '%20') + " target='_blank'>Growth analysis</a>";
    out.push(url);
    var url2 =
      '<a href=/uas_tools/variety_analysis/index.php?crop=Wheat&year=2021&location=Amarillo&sublocation=Irrigation';
    url2 += " target='_blank'>Variety analysis</a>";
    out.push(url2);
    l.bindPopup(out.join('<br />'));
  }
}

I am trying to use out.push(key+": "+f.properties[key].toFixed(2)); but it does not work.

This is the geoJSON file structure

{ "type": "Feature", "properties": { "Row name": "row-1", "Col": "1", "plot_num": "?436", "plot_name": "?AOBS", "join_key": "?AOBS?436", "CC201014": 0.0, "CC201104": 0.0016344676538850001, "CC201120": 0.56401258728343395, "CC201217": 8.3524346613304221, "CC210113": 7.7746312091202094, "CC210224": 9.7393145428079926, "CC210303": 7.673018393542411, "CC210311": 14.576431943872961, "CC210323": 31.081778483525209, "CC210331": 30.067189249720045, "CC210408": 62.738628486108894, "CC210412": 63.94711538461538, "CC210418": 73.721694264987974, "CC210423": 70.039654826897262, "CC210430": 98.045130406889243, "CC210504": 91.969625530436502, "CC210510": 93.321666364934728, "CC210517": 85.521939491083955, "CC210525": 88.782478347768162, "CC210601": 95.859434682964093, "CC210607": 15.974798327739503, "CC210610": 0.0085470085470090006, "CC210614": 0.0, "CC210617": 0.0 }
Ad

Answer

The toFixed() method only works on floats, it appears you may have a string. You could parse it to a float first.

out.push(key + ': ' + parseFloat(f.properties[key]).toFixed(2));
Ad
source: stackoverflow.com
Ad