【发布时间】:2019-11-22 01:06:19
【问题描述】:
我只想在文本框中输入数字。一个数字或带两位小数的数字。
例如,12345 或 1234.56。
我在网上找到了一些代码,但现在找不到资源。那个只适用于一个数字,但在两位小数上失败。我想修改代码。在这里。
currencyChars = new RegExp('[\.,]', 'g'); // we're going to remove commas and dots
constructor(public element: ElementRef, public renderer: Renderer2, private decimalPipe:
DecimalPipe) {}
ngOnInit() {
this.format(this.element.nativeElement.value);
}
@HostListener('input', ["$event.target.value"]) onInput(e: string) {
this.format(e);
}
format(val: string) {
const numberFormat = parseInt(String(val).replace(this.currencyChars, ''));
const usdollar = this.decimalPipe.transform(numberFormat, '1.0', 'en-US');
this.render.setProperty(this.element.nativeElement, 'value', usdollar);
}
我想我必须更改格式方法。请帮忙。
【问题讨论】:
-
需要多少个小数点?
标签: angular