Skip to content

Commit

Permalink
Fix issue with converted integers that ended with 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Cody Hoover committed Feb 28, 2024
1 parent 267635f commit 0908252
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe("convertWorkoutDescriptionUnits", () => {
expect(convertWorkoutDescriptionUnits("123miles", "miles", "kilometers")).toBe("198kilometers");
expect(convertWorkoutDescriptionUnits("123 mi", "miles", "kilometers")).toBe("198 km");
expect(convertWorkoutDescriptionUnits("123mi", "miles", "kilometers")).toBe("198km");
expect(convertWorkoutDescriptionUnits("6 miles", "miles", "kilometers")).toBe("10 kilometers");
expect(convertWorkoutDescriptionUnits("198 kilometers", "kilometers", "miles")).toBe("123 miles");
expect(convertWorkoutDescriptionUnits("198kilometers", "kilometers", "miles")).toBe("123miles");
expect(convertWorkoutDescriptionUnits("198 km", "kilometers", "miles")).toBe("123 mi");
Expand Down
3 changes: 2 additions & 1 deletion src/lib/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ const convertDistance = (input: string, inputUnits: Units, outputUnits: Units):
fixedInput = fixedInput.substring(1);
}

const fixedInputHasDecimal = fixedInput.indexOf(".") >= 0;
// Only preserve a trailing 0 if the input had a trailing zero
if (fixedInput.endsWith("0") && !input.endsWith("0")) {
if (fixedInputHasDecimal && fixedInput.endsWith("0") && !input.endsWith("0")) {
fixedInput = fixedInput.substring(0, fixedInput.length - 1);
}

Expand Down

0 comments on commit 0908252

Please sign in to comment.