Search code examples
htmlangularangular-materialmat

Add decimal value formate for fild in angulare


I Want To Set Number Format Decimal Like 0.00 In Material Filed

My Code Is This

 <div class="col-md-2 col-12 pl-0 ">
   <mat-form-field appearance="outline">
     <input matInput [(ngModel)]="txtOCAmtD"   placeholder="XX.XX"  style="text-align: right;">
   </mat-form-field>
  </div>  

I Add Placeholder But It's Not Work. I Want To Every Value In .00 Decimal If It's .01 or .10 Both Are Required. Any One Tell Me How Can Do It.


Solution

  • you could use something like this:

    <input value="{{ exampleNumber | number : '1.2-2' }}"></input>
    

    '1.2-2' means 'minIntDigits.minDecimalDigits-maxDecimalDigits'

    • 1 digit or more will be shown in front of decimal point
    • At least 2 digits will be shown after decimal point
    • But no more than 2 digits will be shown after decimal point

    Edit: If the above does not work with MatInput you can also add a directive and add it to your html. Kindly refer to https://stackblitz.com/edit/limit-two-digit-decimal-place?file=src%2Fapp%2Fapp.component.html