summaryrefslogtreecommitdiff
path: root/packages/taler-util/src/amounts.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2022-06-09 13:37:33 -0300
committerSebastian <sebasjm@gmail.com>2022-06-09 13:37:33 -0300
commitff49e3477e155b94e752c516cf58fdea1ca19d54 (patch)
treec47cdb74081caf6e703306f8f9f15669d1cdd30b /packages/taler-util/src/amounts.ts
parenteb8bcc95324f3282003b4870d890d0b9f570ee46 (diff)
downloadwallet-core-ff49e3477e155b94e752c516cf58fdea1ca19d54.tar.gz
wallet-core-ff49e3477e155b94e752c516cf58fdea1ca19d54.tar.bz2
wallet-core-ff49e3477e155b94e752c516cf58fdea1ca19d54.zip
format amount so it is align to fractional digitls
Diffstat (limited to 'packages/taler-util/src/amounts.ts')
-rw-r--r--packages/taler-util/src/amounts.ts24
1 files changed, 24 insertions, 0 deletions
diff --git a/packages/taler-util/src/amounts.ts b/packages/taler-util/src/amounts.ts
index 98cd4ad62..d65390a1e 100644
--- a/packages/taler-util/src/amounts.ts
+++ b/packages/taler-util/src/amounts.ts
@@ -444,4 +444,28 @@ export class Amounts {
return s;
}
+
+ /**
+ * Number of fractional digits needed to fully represent the amount
+ * @param a amount
+ * @returns
+ */
+ static maxFractionalDigits(a: AmountJson): number {
+ if (a.fraction === 0) return 0;
+ if (a.fraction < 0) {
+ console.error("amount fraction can not be negative", a);
+ return 0;
+ }
+ let i = 0;
+ let check = true;
+ let rest = a.fraction;
+ while (rest > 0 && check) {
+ check = rest % 10 === 0;
+ rest = rest / 10;
+ i++;
+ }
+ return amountFractionalLength - i + 1;
+ }
+
}
+