diff options
author | Sebastian <sebasjm@gmail.com> | 2022-06-09 13:37:33 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-06-09 13:37:33 -0300 |
commit | ff49e3477e155b94e752c516cf58fdea1ca19d54 (patch) | |
tree | c47cdb74081caf6e703306f8f9f15669d1cdd30b /packages/taler-util | |
parent | eb8bcc95324f3282003b4870d890d0b9f570ee46 (diff) |
format amount so it is align to fractional digitls
Diffstat (limited to 'packages/taler-util')
-rw-r--r-- | packages/taler-util/src/amounts.ts | 24 |
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; + } + } + |