From 0f087a5836a53e5972448bb0647ed852bf075528 Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Mon, 15 Jul 2019 17:11:34 +0200
Subject: [PATCH] Adjust numbers precision in PAB : replaced dirty hack by
 proper round()

---
 .../dialog-generate-pab.component.ts          |  7 ++++---
 .../pab-table/pab-table.component.ts          | 20 +++++++++++++++++--
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/src/app/components/dialog-generate-pab/dialog-generate-pab.component.ts b/src/app/components/dialog-generate-pab/dialog-generate-pab.component.ts
index bb81e269d..dd842382b 100644
--- a/src/app/components/dialog-generate-pab/dialog-generate-pab.component.ts
+++ b/src/app/components/dialog-generate-pab/dialog-generate-pab.component.ts
@@ -5,6 +5,7 @@ import { ApplicationSetupService } from "../../services/app-setup/app-setup.serv
 import { I18nService } from "../../services/internationalisation/internationalisation.service";
 
 import { sprintf } from "sprintf-js";
+import { round } from "jalhyd";
 
 @Component({
     selector: "dialog-generate-pab",
@@ -28,9 +29,9 @@ export class DialogGeneratePABComponent {
     @Inject(MAT_DIALOG_DATA) public data: any
   ) {
     const nDigits = this.appSetupService.displayDigits;
-    this.coteAmont = Number(data.coteAmont.toFixed(nDigits));
-    this.debit = Number(data.debit.toFixed(nDigits));
-    this.chute = Number(data.chute.toFixed(nDigits));
+    this.coteAmont = round(data.coteAmont, nDigits);
+    this.debit = round(data.debit, nDigits);
+    this.chute = round(data.chute, nDigits);
   }
 
   public generatePAB() {
diff --git a/src/app/components/pab-table/pab-table.component.ts b/src/app/components/pab-table/pab-table.component.ts
index cf4561da1..edb78086f 100644
--- a/src/app/components/pab-table/pab-table.component.ts
+++ b/src/app/components/pab-table/pab-table.component.ts
@@ -11,7 +11,8 @@ import {
     Nub,
     Structure,
     ParallelStructure,
-    ParamDefinition
+    ParamDefinition,
+    round
  } from "jalhyd";
 
  import { sprintf } from "sprintf-js";
@@ -435,6 +436,21 @@ export class PabTableComponent implements AfterViewInit, OnInit {
     private refresh() {
         const maxNbDevices = this.findMaxNumberOfDevices();
 
+        // adjuste precision once before anything else
+        const nDigits = this.appSetupService.displayDigits;
+        for (const c of this.model.children) {
+            for (const p of c.parameterIterator) {
+                if (p.visible) {
+                    p.singleValue = round(p.singleValue, nDigits);
+                }
+            }
+        }
+        for (const p of this.model.downWall.parameterIterator) {
+            if (p.visible) {
+                p.singleValue = round(p.singleValue, nDigits);
+            }
+        }
+
         // 0. build spanned headers over real columns
         this.headers = [];
         // 1 header for basin
@@ -1146,7 +1162,7 @@ export class PabTableComponent implements AfterViewInit, OnInit {
                                 let currentValue: number = result.variableDetails.first;
                                 for (let i = 0; i < result.variableDetails.occurrences - 2; i++) {
                                     currentValue += step;
-                                    interpolatedValues.push(Number(currentValue.toFixed(nDigits)));
+                                    interpolatedValues.push(round(currentValue, nDigits));
                                 }
                                 interpolatedValues.push(result.variableDetails.last);
                                 // apply
-- 
GitLab