From c91661743de0e0fb85e92521e170609a566f78bf Mon Sep 17 00:00:00 2001
From: Dimitri DI GUSTO <dimitri.digusto@ausy.fr>
Date: Thu, 10 Feb 2022 15:07:18 +0100
Subject: [PATCH] Round float to 2 decimal on dataset details

---
 .../dataset-data-detail-properties.component.html        | 6 +++---
 .../dataset-data-detail-properties.component.ts          | 9 +++++++++
 .../dataset-table/dataset-table.component.html           | 9 +++------
 .../components/dataset-table/dataset-table.component.ts  | 9 +++++++++
 4 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/src/app/dataset-detail/components/dataset-data-details/dataset-data-detail-properties/dataset-data-detail-properties.component.html b/src/app/dataset-detail/components/dataset-data-details/dataset-data-detail-properties/dataset-data-detail-properties.component.html
index 1bbc3e3f..ae443971 100644
--- a/src/app/dataset-detail/components/dataset-data-details/dataset-data-detail-properties/dataset-data-detail-properties.component.html
+++ b/src/app/dataset-detail/components/dataset-data-details/dataset-data-detail-properties/dataset-data-detail-properties.component.html
@@ -41,10 +41,10 @@
       </div>
       <!-- Display the value. If it is url, display a link -->
       <div *ngSwitchDefault class="property-value">
-        <span
-          *ngIf="!isUrl(properties[key])">{{ (properties[key] && properties[key] !== '') ? properties[key] : '-'  }}</span>
+        <span *ngIf="isFloat(properties[key]) && !isUrl(properties[key])">{{ roundTo(properties[key], 2) }}</span>
+        <span *ngIf="!isFloat(properties[key]) && !isUrl(properties[key])">{{ (properties[key] && properties[key] !== '') ? properties[key] : '-'  }}</span>
         <a [href]="properties[key]" target="_blank" *ngIf="isUrl(properties[key])">{{ properties[key] }}</a>
       </div>
     </ng-container>
   </div>
-</div>
\ No newline at end of file
+</div>
diff --git a/src/app/dataset-detail/components/dataset-data-details/dataset-data-detail-properties/dataset-data-detail-properties.component.ts b/src/app/dataset-detail/components/dataset-data-details/dataset-data-detail-properties/dataset-data-detail-properties.component.ts
index 33efab56..687b0065 100644
--- a/src/app/dataset-detail/components/dataset-data-details/dataset-data-detail-properties/dataset-data-detail-properties.component.ts
+++ b/src/app/dataset-detail/components/dataset-data-details/dataset-data-detail-properties/dataset-data-detail-properties.component.ts
@@ -94,4 +94,13 @@ export class DatasetDataDetailPropertiesComponent implements OnInit {
     const isStringAndNotNull = value ? typeof value === 'string' : false;
     return isStringAndNotNull ? value.match(regex) : false;
   }
+
+  isFloat(n: any) {
+    return Number(n) === n && n % 1 !== 0;
+  }
+
+  roundTo(num: number, places: number) {
+    const factor = 10 ** places;
+    return Math.round(num * factor) / factor;
+  }
 }
diff --git a/src/app/dataset-detail/components/dataset-table/dataset-table.component.html b/src/app/dataset-detail/components/dataset-table/dataset-table.component.html
index 72108716..8d952fae 100644
--- a/src/app/dataset-detail/components/dataset-table/dataset-table.component.html
+++ b/src/app/dataset-detail/components/dataset-table/dataset-table.component.html
@@ -20,12 +20,9 @@
       *ngFor="let key of displayedProperties; let indexRow=index;" (click)="emitSelectedData(element)" [attr.role]="'cell'">
 
       <ng-container *ngIf="!isPropertyComplex(element,key)">
-        <span *ngIf="!isUrl(element.properties[key]); else urlPropTemplate"
-          class="data-property-value">{{ element.properties[key] }}</span>
-        <ng-template #urlPropTemplate>
-          <a class="data-property-value" target="_blank"
-            [href]="element.properties[key]">{{ element.properties[key] }}</a>
-        </ng-template>
+        <span *ngIf="isFloat(element.properties[key]) && !isUrl(element.properties[key])" class="data-property-value">{{ roundTo(element.properties[key], 2) }}</span>
+        <span *ngIf="!isFloat(element.properties[key]) && !isUrl(element.properties[key])" class="data-property-value">{{ element.properties[key] }}</span>
+        <a *ngIf="isUrl(element.properties[key])" [href]="element.properties[key]" target="_blank" class="data-property-value">{{ element.properties[key] }}</a>
       </ng-container>
       <span *ngIf="isPropertyComplex(element,key)">
         <span i18n-data-tooltip="@@dataset.data.tooltip"
diff --git a/src/app/dataset-detail/components/dataset-table/dataset-table.component.ts b/src/app/dataset-detail/components/dataset-table/dataset-table.component.ts
index 8f571dac..4565953b 100644
--- a/src/app/dataset-detail/components/dataset-table/dataset-table.component.ts
+++ b/src/app/dataset-detail/components/dataset-table/dataset-table.component.ts
@@ -166,4 +166,13 @@ export class DatasetTableComponent implements OnInit, OnDestroy {
     // Make there are no duplicates in the array
     this.visitedFeatures = [...new Set(this.visitedFeatures)]
   }
+
+  isFloat(n: any) {
+    return Number(n) === n && n % 1 !== 0;
+  }
+
+  roundTo(num: number, places: number) {
+    const factor = 10 ** places;
+    return Math.round(num * factor) / factor;
+  }
 }
-- 
GitLab