Increase rounding precision, especially speed

This commit is contained in:
Bartek Fabiszewski 2020-02-16 11:09:33 +01:00
parent f9ae256a9f
commit 0f3989a170
6 changed files with 16 additions and 16 deletions

View File

@ -69,21 +69,21 @@ export default class uConfig {
initUnits() {
if (this.units === 'imperial') {
this.factorSpeed = 0.62; // to mph
this.factorSpeed = 2.237; // m/s to mph
this.unitSpeed = 'unitmph';
this.factorDistance = 3.28; // to feet
this.factorDistance = 3.28; // m to feet
this.unitDistance = 'unitft';
this.factorDistanceMajor = 0.62; // to miles
this.factorDistanceMajor = 0.621; // km to miles
this.unitDistanceMajor = 'unitmi';
} else if (this.units === 'nautical') {
this.factorSpeed = 0.54; // to knots
this.factorSpeed = 1.944; // m/s to kt
this.unitSpeed = 'unitkt';
this.factorDistance = 1; // meters
this.unitDistance = 'unitm';
this.factorDistanceMajor = 0.54; // to nautical miles
this.factorDistanceMajor = 0.54; // km to nautical miles
this.unitDistanceMajor = 'unitnm';
} else {
this.factorSpeed = 1;
this.factorSpeed = 3.6; // m/s to km/h
this.unitSpeed = 'unitkmh';
this.factorDistance = 1;
this.unitDistance = 'unitm';

View File

@ -71,7 +71,7 @@ export default class uLang {
* @return {(number|string)} String when with unit
*/
getLocaleSpeed(ms, withUnit) {
const value = Math.round(ms * this.config.factorSpeed * 360) / 100;
const value = Math.round(ms * this.config.factorSpeed * 100) / 100;
if (withUnit) {
return `${value} ${this.unit('unitSpeed')}`;
}

View File

@ -54,7 +54,7 @@ export default class uPosition {
position.latitude = uUtils.getFloat(pos.latitude);
position.longitude = uUtils.getFloat(pos.longitude);
position.altitude = uUtils.getInteger(pos.altitude, true); // may be null
position.speed = uUtils.getInteger(pos.speed, true); // may be null
position.speed = uUtils.getFloat(pos.speed, true); // may be null
position.bearing = uUtils.getInteger(pos.bearing, true); // may be null
position.accuracy = uUtils.getInteger(pos.accuracy, true); // may be null
position.provider = uUtils.getString(pos.provider, true); // may be null

View File

@ -256,7 +256,7 @@ export default class uUtils {
output = parseFloat(input);
break;
case 'int':
output = parseInt(input);
output = Math.round(parseFloat(input));
break;
case 'string':
output = String(input);

View File

@ -60,11 +60,11 @@ describe('Config tests', () => {
config.units = 'imperial';
config.initUnits();
// then
expect(config.factorSpeed).toBe(0.62); // to mph
expect(config.factorSpeed).toBe(2.237); // to mph
expect(config.unitSpeed).toBe('unitmph');
expect(config.factorDistance).toBe(3.28); // to feet
expect(config.unitDistance).toBe('unitft');
expect(config.factorDistanceMajor).toBe(0.62); // to miles
expect(config.factorDistanceMajor).toBe(0.621); // to miles
expect(config.unitDistanceMajor).toBe('unitmi');
});
@ -73,7 +73,7 @@ describe('Config tests', () => {
config.units = 'nautical';
config.initUnits();
// then
expect(config.factorSpeed).toBe(0.54); // to knots
expect(config.factorSpeed).toBe(1.944); // to knots
expect(config.unitSpeed).toBe('unitkt');
expect(config.factorDistance).toBe(1); // meters
expect(config.unitDistance).toBe('unitm');
@ -86,7 +86,7 @@ describe('Config tests', () => {
config.units = 'metric';
config.initUnits();
// then
expect(config.factorSpeed).toBe(1);
expect(config.factorSpeed).toBe(3.6);
expect(config.unitSpeed).toBe('unitkmh');
expect(config.factorDistance).toBe(1);
expect(config.unitDistance).toBe('unitm');
@ -125,7 +125,7 @@ describe('Config tests', () => {
// when
config.load(data);
// then
expect(config.factorSpeed).toBe(0.62);
expect(config.factorSpeed).toBe(2.237);
});
it('should parse regex if present in data', () => {

View File

@ -94,14 +94,14 @@ describe('Lang tests', () => {
// when
lang.init(mockConfig, mockStrings);
// then
expect(lang.getLocaleSpeed(value, false)).toBe(1188);
expect(lang.getLocaleSpeed(value, false)).toBe(330);
});
it('should return localized speed value with unit', () => {
// when
lang.init(mockConfig, mockStrings);
// then
expect(lang.getLocaleSpeed(value, true)).toBe(`1188 ${mockStrings.units}`);
expect(lang.getLocaleSpeed(value, true)).toBe(`330 ${mockStrings.units}`);
});
it('should return localized distance major value', () => {