Skip to content

Commit

Permalink
fix: minPrecision overiding valid digits inside precision range (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizAsFight authored Nov 2, 2022
1 parent 63aa038 commit ec83b17
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-chefs-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/math": patch
---

fix: minPrecision overriding last valid digits
7 changes: 7 additions & 0 deletions packages/math/src/bn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,13 @@ describe('Math - BN', () => {
precision: 8,
})
).toEqual('1,001,000,000.20000');

expect(
bn('100100020000').format({
minPrecision: 4,
precision: 8,
})
).toEqual('100.10002');
});

it('should parse to bn unit from decimal/inputs/string values', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/math/src/decimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export function toFixed(value?: string | number, options?: ToFixedConfig) {

// strip traling zeros limited by minPrecision
if (minPrecision < precision) {
const firstNonZero = decimals.match(/[1-9]/);
const firstNonZeroIndex = firstNonZero?.index == null ? -1 : firstNonZero.index;
const keepChars = Math.max(minPrecision, firstNonZeroIndex + 1);
const trimmedDecimal = decimals.match(/.*[1-9]{1}/);
const lastNonZeroIndex = trimmedDecimal?.[0].length || 0;
const keepChars = Math.max(minPrecision, lastNonZeroIndex);
decimals = decimals.slice(0, keepChars);
}

Expand Down

1 comment on commit ec83b17

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 90.2% 3597/3988
🟡 Branches 72.77% 700/962
🟢 Functions 87.82% 721/821
🟢 Lines 90.21% 3447/3821

Test suite run success

543 tests passing in 49 suites.

Report generated by 🧪jest coverage report action from ec83b17

Please sign in to comment.