首页 > 解决方案 > 如何在我的 formControl 中显示逗号后的 2 位数字

问题描述

例如,我试图让我的输入具有像“1,00”这样的值。但是,我的方法是错误的,它在输入中显示“100”。

这是我的html代码

<mat-form-field appearance="outline">
    <mat-label>valor unitário</mat-label>
    <input matInput formControlName="valor" prefix="R$ " mask="separator.2" thousandSeparator="." (blur)="blurValueInput('valor')" (keyup)="autoComplete('valor')"> 
    <mat-error>campo obrigatório</mat-error>
</mat-form-field>

这是我的打字稿代码。如果我让输入具有这样的值“1,0”,它应该显示“1,00”

constructor(private form: FormBuilder) { }

ngOnInit() {
this.pagamentoForm = this.form.group({
      quantidade: [1, Validators.required],
      desc_produto: ['', Validators.required],
      valor: ['', Validators.required],
      desc_iti: [''],
      desc_varejo: ['']
    });
    this.descInput.nativeElement.focus();
  }

public blurValueInput(labelName: string) {
    if (this.pagamentoForm.controls[labelName].value.length - 
    this.pagamentoForm.controls[labelName].value.indexOf('.') === 2) {        
 this.pagamentoForm.controls[labelName].setValue(Number.parseFloat(this.pagamentoForm.controls[labelName].value).toFixed(2));        
      }
}

在此处输入图像描述

标签: angular

解决方案


推荐阅读