When i'm using parseInt showing an error

Here is my code and error.

 onChangehomeloanloantenure(ev: any) {
   //alert(this.homeloanloantenure);
this.month= (this.homeloanloantenure*12);// here year convert to month 36 
let payments=Math.pow(1+this.monthlyinstrest,this.month);

this.emiammout=(this.homeloanamount*payments*this.monthlyinstrest)/(payments-1);
this.calinstrest=this.emiammout*this.month;//here calculate the total instrest 
this.instrestamount = this.calinstrest-this.homeloanamount;//here calculatet the instrest ammout 
alert(this.emiammout)//EMI
this.totalpay=parseInt(this.homeloanamount)+parseInt(this.instrestamount);// here showing the total pay
}

Error:

please help me anyone.

Hello,
it seems that your variable is a number, but parseint will have a string. So no convert is needed.

Best regards, anna-liebt

but when i’m doing addition operation but its showing concatenation value.

i want to add like 2+2=4 but its showing 22

Hello,
this.totalpay is declared as number and if you write

this.totalpay = 2 + 2;

then totalplay is 22 ?

If this is so…well…hmmmm… maybe someone else have an idea.

Best regards, anna-liebt

Yes!this.totalpay declared as a number. and totalplay is 22. not 2+2=4

Hello,
have you really tried this?
this.totalpay=2+2;
Okay.
and totalplay is declard as number?
Okay.
So the failure message should clear. this.homeloanamount and this.instrestamount are numbers.
Have you check what this both variable contains? For examble set it before calulations to this.homeloanamount =2; and this.instrestamount=3;
Result is 5?

This is working as expected?

this.instrestamount = this.calinstrest-this.homeloanamount;

this.calinstrest is also declared as number and contains what you expected?

Best regards, anna-liebt

Here is my full code please check @anna_liebt

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { SocialSharing } from '@ionic-native/social-sharing';

@IonicPage()
@Component({
  selector: 'page-emi-cal',
  templateUrl: 'emi-cal.html',
})
export class EmiCalPage {
section: string = 'homeloan';
 homeloanamount: number = 0;
 homeloaninterest: number = 0;
 homeloanloantenure: number =0;
 rateofinstrest:number;

 monthlyinstrest:number;
 month:number;
 finalammount:number;
 emiammout:number;
 instrestamount:number;
 totalpay:number;
 calinstrest:number;
constructor(public navCtrl: NavController, public navParams: NavParams,public socialSharing: SocialSharing) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad EmiCalPage');

  }
 onChangehomeloanamount(ev: any) {
  //alert(this.homeloanamount);
}

 onChangehomeloaninterest(ev: any) {
  // alert(this.homeloaninterest);
   this.monthlyinstrest=12/this.homeloaninterest/100;// calculate the monthly instrest 0.012
  
}
 onChangehomeloanloantenure(ev: any) {
   //alert(this.homeloanloantenure);
this.month= (this.homeloanloantenure*12);// here year convert to month 36 
let payments=Math.pow(1+this.monthlyinstrest,this.month);

this.emiammout=(this.homeloanamount*payments*this.monthlyinstrest)/(payments-1);
this.calinstrest=this.emiammout*this.month;//here calculate the total instrest 
this.instrestamount = this.calinstrest-this.homeloanamount;//here calculatet the instrest ammout 
alert(this.emiammout)//EMI
this.totalpay=(this.homeloanamount)+(this.instrestamount);// here showing the total pay
}

}

Hello,
no., I do not check all your code.

For example:

You have
homeloaninterest: number = 0;

And use it in

this.monthlyinstrest=12/this.homeloaninterest/100;

If I am not wrong, than this is a division by zero. So I assume that you anywhere change homeloaninterest.
Maybe it is an idea that you declare variable as string that is used in input, etc. and convert it in .ts explicit with parseInt(mytext, 10)

Maybe formbuilder is a possibility, that have validator, but I have zero experience with that.

Best regards, anna-liebt

Try

this.totalpay=parseInt(this.homeloanamount.toString())+parseInt(this.instrestamount.toString());// here showing the total pay

this.toatlpay = JSON.parse(this.homeloanamount)+ JSON.parse(this.interestamount);
I think this will solve your problem:smiley:

simply try:

this.totalpay = this.homeloanamount + this.instrestamount; // here showing the total pay

just use Number(this.variable) not parseInt