Home > Enterprise >  How to transform a number to two decimal in an ngModel?
How to transform a number to two decimal in an ngModel?

Time:05-17

I have an input with a quantity, I would like to add two decimal in the ngModel

enter image description here

<div >
   <input type="text" id="qte" name="qte" [(ngModel)]="qte ">
</div>

I have a pipe | pipeFormatNum:'1.2-2', but I don't know how to use it in the ngModel?

edit

  qte: number;
  changementBeneficiaire: string = '';
  dest: string = '';


 
  private getSolde(): void {
    this.service.getSolde(this.internalTransactionWatchtoConfirm.titre.svm).pipe(
        takeUntil(this.unsubscribe$)
    ).subscribe(res => {

        if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
            this.qte = res.TRANS[0]["SOLDE"];
            console.log(this.qte);
        }
    });
  }

  onInputChange(event: string) {
    this.qte = Number.parseFloat(event).toFixed(2);
  }

  

CodePudding user response:

You could try with this

<div >
   <input type="text" id="qte" name="qte" [ngModel]="qte" (ngModelChange)="onInputChange($event)">
</div>

Then in your .ts file go with this

onInputChange(event: string) {
  this.qte = Number.parseFloat(event).toFixed(2);
}
  • Related