Search code examples
javascriptnumber-formatting

Geotools decimal places


I'm using geotools.js to convert OS Grid References into Latitudes and Longitudes. Unfortunately the output is by default (I believe) restricted to 2 decimal places. I need it to be 5 decimal places for a more accurate reading.

I have tried removing any reference to decimal places in my own code and also looked through the geotools.js file to see where I could change it, but I'm not smart enough to figure that out!

var osgb = new GT_OSGB();
if (osgb.parseGridRef(gridref)) {
    var wgs84=osgb.getWGS84();
    var lat = parseFloat(Math.round(wgs84.latitude * 100) /     100).toFixed(5);
    var long = parseFloat(Math.round(wgs84.longitude * 100) / 100).toFixed(5);
    var latlong = lat+','+long;
}

Although in the above code I have changed the decimal value from 2 to 5, all that happens is that the decimal places get padded out with zero's. So I would get 2 decimal values and then 3 zero's. Does anyone know how to alter this please?


Solution

  • Because Math.round(1.783895237647 * 100) will result in 178. Dividing that by 100 will not magically recover the lost precision.

    var lat = parseFloat(Math.round(wgs84.latitude * 100) /     100).toFixed(5);
    var long = parseFloat(Math.round(wgs84.longitude * 100) / 100).toFixed(5);
    

    Should probably be

    var lat = parseFloat(wgs84.latitude).toFixed(5);
    var long = parseFloat(wgs84.longitude).toFixed(5);