Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,49 @@ function padZeros(val, length) {
return str;
}

/**
* Round a converted value to fewer decimal places when doing so
* changes the value by less than 1%. For example, 328.08 ft (from 100m)
* becomes "328" and 9842.52 ft becomes "9843". Also rounds to the
* nearest 10/100/etc when within 1 of a boundary (e.g. 999 → "1000").
* Returns a string like toFixed().
*/
function smartRound(value, decimalPlaces) {
if (decimalPlaces < 2 || value === 0) {
return value.toFixed(decimalPlaces);
}
// Try removing decimal places (most aggressive first)
let best = null;
for (let dp = 0; dp <= decimalPlaces - 2; dp++) {
let rounded = parseFloat(value.toFixed(dp));
if (Math.abs(rounded - value) / Math.abs(value) < 0.01) {
best = rounded.toFixed(dp);
break;
}
}
// Try rounding to nearest 10, 100, etc. but only when the integer
// value is within 1 of a boundary (e.g. 999->1000, 1001->1000).
// Use absolute values for boundary detection to handle negatives.
if (best !== null) {
let intVal = Math.round(Math.abs(value));
for (let mag = 1; mag <= 3; mag++) {
let factor = Math.pow(10, mag);
let remainder = intVal % factor;
if (remainder <= 1 || remainder >= factor - 1) {
let rounded = Math.sign(value) * Math.round(Math.abs(value) / factor) * factor;
if (Math.abs(rounded - value) / Math.abs(value) < 0.01) {
best = rounded.toFixed(0);
} else {
break;
}
} else {
break;
}
}
}
return best !== null ? best : value.toFixed(decimalPlaces);
}

var Settings = (function () {
let self = {};

Expand Down Expand Up @@ -534,7 +577,7 @@ var Settings = (function () {
let mins = oldValue - (hours*60);
newValue = ((hours < 0) ? padZeros(hours, 3) : padZeros(hours, 2)) + ':' + padZeros(mins, 2);
} else {
newValue = Number((oldValue / multiplier)).toFixed(decimalPlaces);
newValue = smartRound(Number(oldValue / multiplier), decimalPlaces);
}

element.val(newValue);
Expand Down Expand Up @@ -688,3 +731,4 @@ var Settings = (function () {
})();

export default Settings;
export { smartRound };
6 changes: 3 additions & 3 deletions tabs/osd.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { GUI, TABS } from './../js/gui';
import MSP from './../js/msp';
import MSPCodes from './../js/msp/MSPCodes';
import mspHelper from './../js/msp/MSPHelper';
import Settings from './../js/settings';
import Settings, { smartRound } from './../js/settings';
import { globalSettings } from './../js/globalSettings';
import { PortHandler } from './../js/port_handler';
import i18n from './../js/localization';
Expand Down Expand Up @@ -769,10 +769,10 @@ OSD.constants = {
case 0: // Imperial
case 3: // UK
// meters to miles
return (value / 1609.344).toFixed(2);
return smartRound(value / 1609.344, 2);
case 4: // GA
// metres to nautical miles
return (value / 1852.001).toFixed(2);
return smartRound(value / 1852.001, 2);
default: // Metric
return value;
}
Expand Down