Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mkobetic committed Dec 13, 2024
1 parent f25ff30 commit 2b2a6d5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
7 changes: 4 additions & 3 deletions cmd/coin2html/js/src/commodity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ export class Amount {
const value = Number(parts[0].replace(".", ""));
return new Amount(value, commodity);
}
toString(): string {
toString(thousandsSeparator = true): string {
let str = Math.abs(this.value).toString();
if (this.commodity.decimals > 0) {
if (str.length < this.commodity.decimals) {
str = "0".repeat(this.commodity.decimals - str.length + 1) + str;
}
const intPart = str.slice(0, -this.commodity.decimals);
str =
triplets(str.slice(0, -this.commodity.decimals)).join(",") +
(thousandsSeparator ? triplets(intPart).join(",") : intPart) +
"." +
str.slice(-this.commodity.decimals);
if (str[0] == ".") {
Expand Down Expand Up @@ -196,7 +197,7 @@ export function loadCommodities() {
throw new Error("Unknown commodity: " + imported.commodity);
}
const amount = Amount.parse(imported.value);
if (amount.toString() != imported.value) {
if (amount.toString(false) != imported.value) {
throw new Error(
`Parsed amount "${amount}" doesn't match imported "${imported.value}"`
);
Expand Down
63 changes: 35 additions & 28 deletions examples/yearly/viewer/index.html

Large diffs are not rendered by default.

0 comments on commit 2b2a6d5

Please sign in to comment.