0

Why in the somma value I've the concatenation of totaleEnergetico and totaleStrutturale instead a sum?

RiepilogoCombinatoStComponent.ts

export class RiepilogoCombinatoStComponent implements OnInit {

  constructor() { }

  interventi: AssociazioneInterventoSt[] = []
  arr: number = 0
  selezione: number

  totaleStrutturale: number = 0
  totaleEnergetico: number = 0
  somma: number = 0

  ngOnInit() {
    this.interventi = window.history.state.interventi
    console.log(this.interventi)
  }

  calcola() {
    let totaleSt: number
    this.interventi.forEach(c => {
      this.totaleEnergetico = this.selezione
      totaleSt = c.prezzoSupInt
      this.totaleStrutturale = totaleSt
    })
    this.somma = this.totaleEnergetico + this.totaleStrutturale
    
  }

AssociazioneInterventoSt.ts

prezzoSupInt: number

RiepilogoCombinatoStComponent.html

<div class="container">
    <div class="card my-5" *ngFor="let el of interventi; index as j">
        <div class="card-header" style="text-align: center;">
            <div [(ngModel)]="arr" name="fieldName" ngDefaultControl>
                INTERVENTO: {{el.intervento.codice}}
            </div>
        </div>
    
        <div class="card-body">
            <div *ngIf="interventi[arr]">
                <select class="form-control" [(ngModel)]="selezione">
                    <option [value]="int.prezzoVista" *ngFor="let int of interventi[arr].associazione; index as i">
                        {{int.intervento.codice}}
                    </option>
                </select>
                <div style="text-align: center; margin-top: 10px;">
                    <button class="btn btn-outline-default" (click)="calcola()">Ottieni risultato</button>
                </div>
                <hr>
                Costo Strutturale: {{el.prezzoSupInt}}<br>
                Costo Energetico: {{totaleEnergetico}}
                <hr>
                Somma: {{somma}}<br>
                Costo Integrato:<br>
                <hr>
                Risparmio euro:<br>
                Risparmio %:<br>
                <hr>
                Attrezzature per la sicurezza: 
            </div> 
        </div>
    </div>    
</div>
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • Probably because `[(ngModel)]="selezione"` gives you a string, because it comes from ` – Jeremy Thille Apr 09 '21 at 12:17
  • Does this answer your question? [Adding two numbers concatenates them instead of calculating the sum](https://stackoverflow.com/questions/14496531/adding-two-numbers-concatenates-them-instead-of-calculating-the-sum) – Heretic Monkey Apr 09 '21 at 12:28

1 Answers1

0

Try to change this:

this.somma = this.totaleEnergetico + this.totaleStrutturale;

To this:

this.somma = (+this.totaleEnergetico) + (+this.totaleStrutturale);
StPaulis
  • 2,844
  • 1
  • 14
  • 24