From ff49e3477e155b94e752c516cf58fdea1ca19d54 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 9 Jun 2022 13:37:33 -0300 Subject: format amount so it is align to fractional digitls --- packages/taler-util/src/amounts.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'packages/taler-util') 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; + } + } + -- cgit v1.2.3