Skip to content

Commit

Permalink
fix: fixed wrong rounding to 0 decimal places
Browse files Browse the repository at this point in the history
  • Loading branch information
Quacken8 committed Sep 4, 2024
1 parent c4bb814 commit c0d9cb7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "unitlib",
"version": "0.8.8",
"version": "0.8.9",
"description": "A unit library",
"type": "module",
"repository": {
Expand Down Expand Up @@ -54,4 +54,4 @@
"fraction.js": "^4.2.1"
},
"packageManager": "[email protected]+sha512.98a80fd11c2e7096747762304106432b3ddc67dcf54b5a8c01c93f68a2cd5e05e6821849522a06fb76284d41a2660d5e334f2ee3bbf29183bf2e739b1dafa771"
}
}
6 changes: 6 additions & 0 deletions src/Quantity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,11 @@ describe('Quantity', () => {
const q = new Quantity(1, SI.parseUnit('km'));
expect(q.toString()).toBe('1 km');
});
test('should properly take options into account', () => {
let q = new Quantity(1.88, SI.parseUnit('m'));
expect(q.toString({ decimalPlaces: 1 })).toBe('1.9 m');
q = new Quantity(1.8, SI.parseUnit('m'));
expect(q.toString({ decimalPlaces: 0 })).toBe('2 m');
});
});
});
5 changes: 4 additions & 1 deletion src/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ export function formatFloat(

const fractionalPart = Math.abs(n) % 1;
const fractionalDigits = BigInt(
roundFloat(fractionalPart * 10 ** decimalPlaces, roundingStrategy),
roundFloat(
fractionalPart * 10 ** decimalPlaces,
decimalPlaces === 0 ? 'down' : roundingStrategy,
),
);

const result: string[] = [];
Expand Down

0 comments on commit c0d9cb7

Please sign in to comment.