commit 0a3410bee4c3e0a7f7a6d13cdccf92465aebf344
parent a3f091ec6dafa32b0c0d5d00020a03e03686ba38
Author: Sergey Orlov <wladimirych@gmail.com>
Date: Mon, 2 Nov 2020 21:07:31 +0100
coordinates: fix rounding errors for DM and DMS formats
Number.prototype.toFixed rounds the value which can cause result like
60.00 minutes when the value is slightly less than integer value.
Diffstat:
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/src/lib/leaflet.control.coordinates/formats.js b/src/lib/leaflet.control.coordinates/formats.js
@@ -1,13 +1,28 @@
+function truncToFixed(x, precision) {
+ let s = x.toString();
+ let point = s.indexOf('.');
+ if (point === -1) {
+ point = s.length;
+ s += '.';
+ }
+ let sliceEnd = point + precision;
+ if (precision > 0) {
+ s = s.padEnd(point + precision + 1, '0');
+ sliceEnd += 1;
+ }
+ s = s.slice(0, sliceEnd);
+ return s;
+}
+
function formatNumber(value, size, precision = 0) {
if (value < 0) {
- return value.toFixed(precision);
+ throw new Error('Negative values not supported');
}
if (precision > 0) {
size += 1;
}
-
- return value.toFixed(precision).padStart(size + precision, '0');
+ return truncToFixed(value, precision).padStart(size + precision, '0');
}
function coordinatePresentations(coordinate, isLat) {
@@ -25,7 +40,7 @@ function coordinatePresentations(coordinate, isLat) {
}
return {
- signedDegrees: formatNumber(coordinate, 0, 5),
+ signedDegrees: coordinate.toFixed(5),
degrees: formatNumber(degrees, 0, 5),
intDegrees: formatNumber(intDegrees, 0),
minutes: formatNumber(minutes, 2, 3),