From 2c30b53158478cc854ce3347b60a1dbf11952a02 Mon Sep 17 00:00:00 2001
From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com>
Date: Sat, 20 Jul 2024 17:32:48 +0200
Subject: [PATCH] Improve handling of numerical precision in value component
---
.../admin-overview/admin-overview.html | 7 +----
libs/ui/src/lib/value/value.component.ts | 29 +++++++++++++------
2 files changed, 21 insertions(+), 15 deletions(-)
diff --git a/apps/client/src/app/components/admin-overview/admin-overview.html b/apps/client/src/app/components/admin-overview/admin-overview.html
index 5c052f21f..a1bce7616 100644
--- a/apps/client/src/app/components/admin-overview/admin-overview.html
+++ b/apps/client/src/app/components/admin-overview/admin-overview.html
@@ -12,11 +12,7 @@
@@ -24,7 +20,6 @@
@if (transactionCount && userCount) {
diff --git a/libs/ui/src/lib/value/value.component.ts b/libs/ui/src/lib/value/value.component.ts
index 71bdfc7c3..47730d466 100644
--- a/libs/ui/src/lib/value/value.component.ts
+++ b/libs/ui/src/lib/value/value.component.ts
@@ -58,8 +58,10 @@ export class GfValueComponent implements OnChanges {
this.formattedValue = this.absoluteValue.toLocaleString(
this.locale,
{
- maximumFractionDigits: this.precision,
- minimumFractionDigits: this.precision
+ maximumFractionDigits:
+ this.precision >= 0 ? this.precision : 2,
+ minimumFractionDigits:
+ this.precision >= 0 ? this.precision : 2
}
);
} catch {}
@@ -68,8 +70,10 @@ export class GfValueComponent implements OnChanges {
this.formattedValue = (this.absoluteValue * 100).toLocaleString(
this.locale,
{
- maximumFractionDigits: this.precision,
- minimumFractionDigits: this.precision
+ maximumFractionDigits:
+ this.precision >= 0 ? this.precision : 2,
+ minimumFractionDigits:
+ this.precision >= 0 ? this.precision : 2
}
);
} catch {}
@@ -77,8 +81,8 @@ export class GfValueComponent implements OnChanges {
} else if (this.isCurrency) {
try {
this.formattedValue = this.value?.toLocaleString(this.locale, {
- maximumFractionDigits: this.precision,
- minimumFractionDigits: this.precision
+ maximumFractionDigits: this.precision >= 0 ? this.precision : 2,
+ minimumFractionDigits: this.precision >= 0 ? this.precision : 2
});
} catch {}
} else if (this.isPercent) {
@@ -86,11 +90,18 @@ export class GfValueComponent implements OnChanges {
this.formattedValue = (this.value * 100).toLocaleString(
this.locale,
{
- maximumFractionDigits: this.precision,
- minimumFractionDigits: this.precision
+ maximumFractionDigits: this.precision >= 0 ? this.precision : 2,
+ minimumFractionDigits: this.precision >= 0 ? this.precision : 2
}
);
} catch {}
+ } else if (this.precision >= 0) {
+ try {
+ this.formattedValue = this.value?.toLocaleString(this.locale, {
+ maximumFractionDigits: this.precision,
+ minimumFractionDigits: this.precision
+ });
+ } catch {}
} else {
this.formattedValue = this.value?.toLocaleString(this.locale);
}
@@ -129,7 +140,7 @@ export class GfValueComponent implements OnChanges {
this.isNumber = false;
this.isString = false;
this.locale = this.locale || getLocale();
- this.precision = this.precision >= 0 ? this.precision : 2;
+ this.precision = this.precision >= 0 ? this.precision : undefined;
this.useAbsoluteValue = false;
}
}