round to whole seconds

the steps are serialized as f32, and the resulting imprecision was
leading to decimal values

closes #1203
This commit is contained in:
Damien Elmes 2021-05-28 19:34:25 +10:00
parent dcb7dc41de
commit 4c2a598be4
2 changed files with 7 additions and 1 deletions

View File

@ -9,6 +9,7 @@ test("whole steps", () => {
expect(stepsToString(steps)).toBe(string); expect(stepsToString(steps)).toBe(string);
expect(stringToSteps(string)).toStrictEqual(steps); expect(stringToSteps(string)).toStrictEqual(steps);
}); });
test("fractional steps", () => { test("fractional steps", () => {
const steps = [1 / 60, 5 / 60, 1.5, 400]; const steps = [1 / 60, 5 / 60, 1.5, 400];
const string = "1s 5s 90s 400m"; const string = "1s 5s 90s 400m";
@ -16,6 +17,11 @@ test("fractional steps", () => {
expect(stringToSteps(string)).toStrictEqual(steps); expect(stringToSteps(string)).toStrictEqual(steps);
}); });
test("rounding", () => {
const steps = [0.1666666716337204];
expect(stepsToString(steps)).toBe("10s");
});
test("parsing", () => { test("parsing", () => {
expect(stringToSteps("")).toStrictEqual([]); expect(stringToSteps("")).toStrictEqual([]);
expect(stringToSteps(" ")).toStrictEqual([]); expect(stringToSteps(" ")).toStrictEqual([]);

View File

@ -38,7 +38,7 @@ function minutesToString(step: number): string {
if ([TimespanUnit.Months, TimespanUnit.Years].includes(unit)) { if ([TimespanUnit.Months, TimespanUnit.Years].includes(unit)) {
unit = TimespanUnit.Days; unit = TimespanUnit.Days;
} }
const amount = unitAmount(unit, secs); const amount = Math.round(unitAmount(unit, secs));
return `${amount}${unitSuffix(unit)}`; return `${amount}${unitSuffix(unit)}`;
} }