Skip to content

Commit

Permalink
amount.display: Don't add trailing zeros by default (#696)
Browse files Browse the repository at this point in the history
* don't add trailing zeros by default

* put padEnd back

* fix test
  • Loading branch information
artursapek committed Sep 13, 2024
1 parent cc51d7c commit d61cc30
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/base/__tests__/amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe("Amount Tests", function () {
[{ amount: "1", decimals: 18 }, 20, "0.00000000000000000100"],
[{ amount: "5020", decimals: 2 }, 0, "50.2"],
[{ amount: "5020", decimals: 2 }, 4, "50.2000"],
[{ amount: "5020", decimals: 2 }, undefined, "50.20"],
[{ amount: "5020", decimals: 2 }, undefined, "50.2"],
[{ amount: "1", decimals: 0 }, 0, "1"],
];

Expand Down
4 changes: 4 additions & 0 deletions core/base/src/utils/amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ export function display(amount: Amount, precision?: number): string {
partial = partial.substring(0, partial.length - 1);
}
partial = partial.padEnd(precision, "0");
} else {
// If no specific precision is given, just trim trailing zeroes
// and return all significant digits.
partial = partial.replace(/0+$/, '');
}

return partial.length > 0 ? `${whole}.${partial}` : whole;
Expand Down

0 comments on commit d61cc30

Please sign in to comment.