diff --git a/src/app/components/fixedvar-results/fixed-results.component.ts b/src/app/components/fixedvar-results/fixed-results.component.ts index 6a38cb84eb649a562e81824f6f98a479291ab40a..dfee24485714ac7e8c6b2358b17b525b66d2542c 100644 --- a/src/app/components/fixedvar-results/fixed-results.component.ts +++ b/src/app/components/fixedvar-results/fixed-results.component.ts @@ -4,10 +4,10 @@ import { FixedResults } from "../../results/fixed-results"; import { NgParameter } from "../../formulaire/ngparam"; import { CalculatorResults } from "../../results/calculator-results"; import { I18nService } from "../../services/internationalisation/internationalisation.service"; -import { ApplicationSetupService } from "../../services/app-setup/app-setup.service"; import * as XLSX from "xlsx"; import { Structure } from "jalhyd"; import { FormulaireService } from "../../services/formulaire/formulaire.service"; +import { ResultsComponent } from "./results.component"; @Component({ selector: "fixed-results", @@ -16,7 +16,7 @@ import { FormulaireService } from "../../services/formulaire/formulaire.service" "./fixed-results.component.scss" ] }) -export class FixedResultsComponent { +export class FixedResultsComponent extends ResultsComponent { /** résultats non mis en forme */ private _fixedResults: FixedResults; @@ -27,9 +27,10 @@ export class FixedResultsComponent { constructor( private intlService: I18nService, - private appSetupService: ApplicationSetupService, private formService: FormulaireService - ) { } + ) { + super(); + } public set results(r: FixedResults) { this._fixedResults = r; @@ -63,11 +64,6 @@ export class FixedResultsComponent { return CalculatorResults.paramLabel(p, false); } - private formattedValue(p: NgParameter): string { - const nDigits = this.appSetupService.displayDigits; - return p.getValue().toFixed(nDigits); - } - /** * Returns a set of parameters and results for mat-table */ diff --git a/src/app/components/fixedvar-results/fixedvar-results.component.ts b/src/app/components/fixedvar-results/fixedvar-results.component.ts index 87a483a11aa5a8a2773beb7dbb95767567872fb8..0c699773bebe5239e66ccc393fb685c8e5442f9e 100644 --- a/src/app/components/fixedvar-results/fixedvar-results.component.ts +++ b/src/app/components/fixedvar-results/fixedvar-results.component.ts @@ -7,9 +7,9 @@ import { ResultsGraphComponent } from "../results-graph/results-graph.component" import { CalculatorResults } from "../../results/calculator-results"; import { Result, cLog } from "jalhyd"; import { NgParameter } from "../../formulaire/ngparam"; -import { ApplicationSetupService } from "../../services/app-setup/app-setup.service"; import { FixedResultsComponent } from "./fixed-results.component"; import { VarResultsComponent } from "./var-results.component"; +import { ResultsComponent } from "./results.component"; @Component({ selector: "fixedvar-results", @@ -18,7 +18,7 @@ import { VarResultsComponent } from "./var-results.component"; "./fixedvar-results.component.scss" ] }) -export class FixedVarResultsComponent implements DoCheck { +export class FixedVarResultsComponent extends ResultsComponent implements DoCheck { /** * résultats non mis en forme */ @@ -48,10 +48,6 @@ export class FixedVarResultsComponent implements DoCheck { @ViewChild(ResultsGraphComponent, { static: false }) private resultsGraphComponent: ResultsGraphComponent; - constructor( - private appSetupService: ApplicationSetupService, - ) { } - public set results(rs: CalculatorResults[]) { this._fixedResults = undefined; this._varResults = undefined; @@ -183,11 +179,6 @@ export class FixedVarResultsComponent implements DoCheck { return CalculatorResults.paramLabel(p, false); } - private formattedValue(p: NgParameter): string { - const nDigits = this.appSetupService.displayDigits; - return p.getValue().toFixed(nDigits); - } - public get varResults() { return this._varResults; } diff --git a/src/app/components/fixedvar-results/results.component.ts b/src/app/components/fixedvar-results/results.component.ts index 2675d065e16b3cb8c9279656349e9bf66ed3f2d0..4f45d632e6884beb5d0d9f3b23d45158e8d2d418 100644 --- a/src/app/components/fixedvar-results/results.component.ts +++ b/src/app/components/fixedvar-results/results.component.ts @@ -1,6 +1,9 @@ import * as screenfull from "screenfull"; import { Screenfull } from "screenfull"; // @see https://github.com/sindresorhus/screenfull.js/issues/126#issuecomment-488796645 +import { NgParameter } from "../../formulaire/ngparam"; +import { ServiceFactory } from "../../services/service-factory"; + /** * Base class for results components, including common features */ @@ -52,4 +55,34 @@ export class ResultsComponent { "#469990", // teal ]; } + + /** + * Formats (rounds) the given number (or the value of the given parameter) with the + * number of decimals specified in app preferences; if given number is too low and + * more decimals would be needed, keep as much significative digits as the number of + * decimals specified in app preferences, except potential trailing zeroes + * + * ex. with 3 decimals: + * 1 => 1.000 + * 65431 => 65431.000 + * 0.002 => 0.002 + * 0.00004521684 => 0.0000452 + * 0.000001 => 0.000001 + */ + protected formattedValue(p: NgParameter | number): string { + let originalValue: number; + if (p instanceof NgParameter) { + originalValue = p.getValue(); + } else if (typeof p === "number") { + originalValue = p; + } + const nDigits = ServiceFactory.instance.applicationSetupService.displayDigits; + const minRenderableNumber = Number("1E-" + nDigits); + // if required precision is too low, avoid rendering only zeroes + if (originalValue < minRenderableNumber) { + return String(Number(originalValue.toPrecision(nDigits))); // double casting avoids trailing zeroes + } else { + return originalValue.toFixed(nDigits); + } + } } diff --git a/src/app/components/fixedvar-results/var-results.component.ts b/src/app/components/fixedvar-results/var-results.component.ts index 5eb499f01060bb0de3af6e671585eb2ee06ef59c..6afa70b5f7e243fb8eecf7410b039a9c84a9b4ac 100644 --- a/src/app/components/fixedvar-results/var-results.component.ts +++ b/src/app/components/fixedvar-results/var-results.component.ts @@ -5,7 +5,6 @@ import { MatDialog } from "@angular/material"; import * as XLSX from "xlsx"; import { VarResults } from "../../results/var-results"; -import { ApplicationSetupService } from "../../services/app-setup/app-setup.service"; import { ResultElement, Message, MessageSeverity } from "jalhyd"; import { I18nService } from "../../services/internationalisation/internationalisation.service"; import { ResultsComponent } from "./results.component"; @@ -39,7 +38,6 @@ export class VarResultsComponent extends ResultsComponent { table: ElementRef; constructor( - protected appSetupService: ApplicationSetupService, protected intlService: I18nService, protected logEntriesDetailsDialog: MatDialog ) { @@ -53,8 +51,6 @@ export class VarResultsComponent extends ResultsComponent { this._headers = []; this._messages = []; - const nDigits = this.appSetupService.displayDigits; - if (this._varResults) { const sn = this._varResults.result.sourceNub; // A. gather messages @@ -87,7 +83,7 @@ export class VarResultsComponent extends ResultsComponent { const iter = v.getExtendedValuesIterator(this.size); while (iter.hasNext) { const nv = iter.next(); - vv.push(nv.value.toFixed(nDigits)); + vv.push(this.formattedValue(nv.value)); } varValues.push(vv); } diff --git a/src/app/components/section-results/section-results.component.ts b/src/app/components/section-results/section-results.component.ts index dc00f2a1c413f6ebc25f5210994e624b50b626a6..d927c09f1328a08856c3fde5dd53bf63bf291a40 100644 --- a/src/app/components/section-results/section-results.component.ts +++ b/src/app/components/section-results/section-results.component.ts @@ -4,7 +4,6 @@ import { ResultElement } from "jalhyd"; import { SectionCanvasComponent } from "../section-canvas/section-canvas.component"; import { SectionResults } from "../../results/section-results"; -import { ApplicationSetupService } from "../../services/app-setup/app-setup.service"; import { CalculatorResults } from "../../results/calculator-results"; import { ResultsComponent } from "../fixedvar-results/results.component"; import { I18nService } from "../../services/internationalisation/internationalisation.service"; @@ -19,7 +18,6 @@ import { I18nService } from "../../services/internationalisation/internationalis export class SectionResultsComponent extends ResultsComponent implements DoCheck { constructor( - private appSetupService: ApplicationSetupService, private intlService: I18nService ) { super(); @@ -100,7 +98,6 @@ export class SectionResultsComponent extends ResultsComponent implements DoCheck private updateResults() { if (this._results && this._sectionCanvas) { - const nDigits = this.appSetupService.displayDigits; this._resultElement = new ResultElement(); @@ -113,7 +110,7 @@ export class SectionResultsComponent extends ResultsComponent implements DoCheck this._resultElement.addExtraResult(lbl, er); if (this.isSectionLevel(k)) { - this._sectionCanvas.addLevel(er, k + " = " + er.toFixed(nDigits), SectionResultsComponent.labelColors[k]); + this._sectionCanvas.addLevel(er, k + " = " + this.formattedValue(er), SectionResultsComponent.labelColors[k]); } } } diff --git a/src/app/services/internationalisation/internationalisation.service.ts b/src/app/services/internationalisation/internationalisation.service.ts index 7590f251dc3410941b9a04f7e86204ec92d69636..65a77c2788bec56dcfb05ae2d6874881e29cb5b9 100644 --- a/src/app/services/internationalisation/internationalisation.service.ts +++ b/src/app/services/internationalisation/internationalisation.service.ts @@ -153,7 +153,6 @@ export class I18nService extends Observable implements Observer { * @param nDigits nombre de chiffres à utiliser pour l'arrondi dans le cas de données numériques */ public localizeMessage(r: Message, nDigits: number = 3): string { - const sCode: string = MessageCode[r.code]; let m: string = this.getMessageFromCode(r.code); for (const k in r.extraVar) {