Skip to content

Commit

Permalink
Update L.Util.formatNum rounding (Leaflet#6587)
Browse files Browse the repository at this point in the history
* Update L.Util.formatNum rounding

* Update doc: add precision argument to toGeoJSON()
  • Loading branch information
inkstak authored and Schleuse committed Dec 3, 2019
1 parent fca869f commit 478970f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
18 changes: 9 additions & 9 deletions docs/reference-1.4.0.html
Expand Up @@ -2543,7 +2543,7 @@ <h3 id='marker-method'>Methods</h3>
</tr>
</thead><tbody>
<tr id='marker-togeojson'>
<td><code><b>toGeoJSON</b>()</nobr></code></td>
<td><code><b>toGeoJSON</b>(<nobr>&lt;Number&gt;</nobr> <i>precision</i>)</nobr></code></td>
<td><code>Object</code></td>
<td><p>Returns a <a href="http://en.wikipedia.org/wiki/GeoJSON"><a href="#geojson"><code>GeoJSON</code></a></a> representation of the marker (as a GeoJSON <a href="#point"><code>Point</code></a> Feature).</p>
</td>
Expand Down Expand Up @@ -8702,7 +8702,7 @@ <h3 id='polyline-method'>Methods</h3>
</tr>
</thead><tbody>
<tr id='polyline-togeojson'>
<td><code><b>toGeoJSON</b>()</nobr></code></td>
<td><code><b>toGeoJSON</b>(<nobr>&lt;Number&gt;</nobr> <i>precision</i>)</nobr></code></td>
<td><code>Object</code></td>
<td><p>Returns a <a href="http://en.wikipedia.org/wiki/GeoJSON"><a href="#geojson"><code>GeoJSON</code></a></a> representation of the polyline (as a GeoJSON <code>LineString</code> or <code>MultiLineString</code> Feature).</p>
</td>
Expand Down Expand Up @@ -9568,7 +9568,7 @@ <h3 id='polygon-method'>Methods</h3>
</tr>
</thead><tbody>
<tr id='polygon-togeojson'>
<td><code><b>toGeoJSON</b>()</nobr></code></td>
<td><code><b>toGeoJSON</b>(<nobr>&lt;Number&gt;</nobr> <i>precision</i>)</nobr></code></td>
<td><code>Object</code></td>
<td><p>Returns a <a href="http://en.wikipedia.org/wiki/GeoJSON"><a href="#geojson"><code>GeoJSON</code></a></a> representation of the polygon (as a GeoJSON <a href="#polygon"><code>Polygon</code></a> or <code>MultiPolygon</code> Feature).</p>
</td>
Expand Down Expand Up @@ -10464,7 +10464,7 @@ <h3 id='rectangle-method'>Methods</h3>
</tr>
</thead><tbody>
<tr id='rectangle-togeojson'>
<td><code><b>toGeoJSON</b>()</nobr></code></td>
<td><code><b>toGeoJSON</b>(<nobr>&lt;Number&gt;</nobr> <i>precision</i>)</nobr></code></td>
<td><code>Object</code></td>
<td><p>Returns a <a href="http://en.wikipedia.org/wiki/GeoJSON"><a href="#geojson"><code>GeoJSON</code></a></a> representation of the polygon (as a GeoJSON <a href="#polygon"><code>Polygon</code></a> or <code>MultiPolygon</code> Feature).</p>
</td>
Expand Down Expand Up @@ -11361,7 +11361,7 @@ <h3 id='circle-method'>Methods</h3>
</tr>
</thead><tbody>
<tr id='circle-togeojson'>
<td><code><b>toGeoJSON</b>()</nobr></code></td>
<td><code><b>toGeoJSON</b>(<nobr>&lt;Number&gt;</nobr> <i>precision</i>)</nobr></code></td>
<td><code>Object</code></td>
<td><p>Returns a <a href="http://en.wikipedia.org/wiki/GeoJSON"><a href="#geojson"><code>GeoJSON</code></a></a> representation of the circle marker (as a GeoJSON <a href="#point"><code>Point</code></a> Feature).</p>
</td>
Expand Down Expand Up @@ -12143,7 +12143,7 @@ <h3 id='circlemarker-method'>Methods</h3>
</tr>
</thead><tbody>
<tr id='circlemarker-togeojson'>
<td><code><b>toGeoJSON</b>()</nobr></code></td>
<td><code><b>toGeoJSON</b>(<nobr>&lt;Number&gt;</nobr> <i>precision</i>)</nobr></code></td>
<td><code>Object</code></td>
<td><p>Returns a <a href="http://en.wikipedia.org/wiki/GeoJSON"><a href="#geojson"><code>GeoJSON</code></a></a> representation of the circle marker (as a GeoJSON <a href="#point"><code>Point</code></a> Feature).</p>
</td>
Expand Down Expand Up @@ -13922,7 +13922,7 @@ <h3 id='layergroup-method'>Methods</h3>
</tr>
</thead><tbody>
<tr id='layergroup-togeojson'>
<td><code><b>toGeoJSON</b>()</nobr></code></td>
<td><code><b>toGeoJSON</b>(<nobr>&lt;Number&gt;</nobr> <i>precision</i>)</nobr></code></td>
<td><code>Object</code></td>
<td><p>Returns a <a href="http://en.wikipedia.org/wiki/GeoJSON"><a href="#geojson"><code>GeoJSON</code></a></a> representation of the layer group (as a GeoJSON <code>FeatureCollection</code>, <code>GeometryCollection</code>, or <code>MultiPoint</code>).</p>
</td>
Expand Down Expand Up @@ -14597,7 +14597,7 @@ <h3 id='featuregroup-method'>Methods</h3>
</tr>
</thead><tbody>
<tr id='featuregroup-togeojson'>
<td><code><b>toGeoJSON</b>()</nobr></code></td>
<td><code><b>toGeoJSON</b>(<nobr>&lt;Number&gt;</nobr> <i>precision</i>)</nobr></code></td>
<td><code>Object</code></td>
<td><p>Returns a <a href="http://en.wikipedia.org/wiki/GeoJSON"><a href="#geojson"><code>GeoJSON</code></a></a> representation of the layer group (as a GeoJSON <code>FeatureCollection</code>, <code>GeometryCollection</code>, or <code>MultiPoint</code>).</p>
</td>
Expand Down Expand Up @@ -15381,7 +15381,7 @@ <h3 id='geojson-method'>Methods</h3>
</tr>
</thead><tbody>
<tr id='geojson-togeojson'>
<td><code><b>toGeoJSON</b>()</nobr></code></td>
<td><code><b>toGeoJSON</b>(<nobr>&lt;Number&gt;</nobr> <i>precision</i>)</nobr></code></td>
<td><code>Object</code></td>
<td><p>Returns a <a href="http://en.wikipedia.org/wiki/GeoJSON"><a href="#geojson"><code>GeoJSON</code></a></a> representation of the layer group (as a GeoJSON <code>FeatureCollection</code>, <code>GeometryCollection</code>, or <code>MultiPoint</code>).</p>
</td>
Expand Down
3 changes: 3 additions & 0 deletions spec/suites/core/UtilSpec.js
Expand Up @@ -86,6 +86,9 @@ describe('Util', function () {
expect(L.Util.formatNum(13.12325555, 3)).to.eql(13.123);
expect(L.Util.formatNum(13.12325555)).to.eql(13.123256);
expect(L.Util.formatNum(13.12325555, 0)).to.eql(13);
expect(L.Util.formatNum(1.005, 2)).to.eql(1.01);
expect(L.Util.formatNum(1.555, 2)).to.eql(1.56);
expect(L.Util.formatNum(-1.4837191022531273, 18)).to.eql(-1.4837191022531273);
});
});

Expand Down
6 changes: 6 additions & 0 deletions spec/suites/layer/GeoJSONSpec.js
Expand Up @@ -80,6 +80,12 @@ describe("L.Marker#toGeoJSON", function () {
type: 'Point',
coordinates: [20.123, 10.123, 30.123]
});

marker = new L.Marker({lng: -1.4837191022531273, lat: 43.49222084042808});
expect(marker.toGeoJSON(18).geometry).to.eql({
type: 'Point',
coordinates: [-1.4837191022531273, 43.49222084042808]
});
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/core/Util.js
Expand Up @@ -114,8 +114,8 @@ export function falseFn() { return false; }
// @function formatNum(num: Number, digits?: Number): Number
// Returns the number `num` rounded to `digits` decimals, or to 6 decimals by default.
export function formatNum(num, digits) {
var pow = Math.pow(10, (digits === undefined ? 6 : digits));
return Math.round(num * pow) / pow;
digits = (digits === undefined ? 6 : digits);
return +(Math.round(num + ('e+' + digits)) + ('e-' + digits));
}

// @function trim(str: String): String
Expand Down

0 comments on commit 478970f

Please sign in to comment.