I found that using the percentpipe was no good for inputs.
I am storing the VAT(Tax rate) rate as a float in my db, but I wanted to show this as a percentage on the product edit form. I suspect it would be confusing otherwise.
So the percentpipe isn't a great fit as it transforms numbers into text strings. Now I guess I could have transformed the number back on save, but I wanted to use the number spinner in the input and to handle the validation.
So this is what I ended up with...
1) in the form I have a input
<mat-form-field class="col-12 col-sm-6 col-md-2" >
<mat-label>VAT rate </mat-label>
<input type="number" matInput formControlName="prodVatRate"
min="0" max="100" pattern ="^0?[0-9]?[0-9]$|^(100)$">
<span matPrefix>% </span>
</mat-form-field>
2) in the component I have a couple of simple functions to handle the transform.
toPercent(floatingNumber: number): number {
return floatingNumber * 100;
}
toDecimal(integerNumber: number): number {
return integerNumber / 100;
}
they are so simple you might want to add them inline.
3) I'm using reactive forms so at page load I "patchValue" the form with the http response...
this.productForm.patchValue({
prodVatRate: this.toPercent(this.product.prodVatRate),
... more data inserted here
});
4) and finally in my form save / submit, I transform the data back to a float.
const p = { ...this.product, ...this.productForm.value };
// change the Vat rate back to a floating point number
p.prodVatRate = this.toDecimal(p.prodVatRate);
I hope it helps you out.