- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
代码如下
<input type="number" class="form-control" value="" name="cost_price" #name="ngModel" [(ngModel)]="item.cost_price" placeholder="Cost Price" />
用户不能输入超过 2 位小数。
例如,如果用户要输入 21.256。应该只允许他进入21.25
如何使用 angular 5 实现这一点?
最佳答案
首先创建指令来限制 typescript 中的两位小数,如下所示:
import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: '[appTwoDigitDecimaNumber]'
})
export class TwoDigitDecimaNumberDirective {
private regex: RegExp = new RegExp(/^\d*\.?\d{0,2}$/g);
private specialKeys: Array<string> = ['Backspace', 'Tab', 'End', 'Home', '-', 'ArrowLeft', 'ArrowRight', 'Del', 'Delete'];
constructor(private el: ElementRef) {
}
@HostListener('keydown', ['$event'])
onKeyDown(event: KeyboardEvent) {
console.log(this.el.nativeElement.value);
// Allow Backspace, tab, end, and home keys
if (this.specialKeys.indexOf(event.key) !== -1) {
return;
}
let current: string = this.el.nativeElement.value;
const position = this.el.nativeElement.selectionStart;
const next: string = [current.slice(0, position), event.key == 'Decimal' ? '.' : event.key, current.slice(position)].join('');
if (next && !String(next).match(this.regex)) {
event.preventDefault();
}
}
}
在您的 app.module.ts
中注入(inject)指令。在你的 html 中使用这样的指令:
<input type="textbox" [(ngModel)]="InputValue" appTwoDigitDecimaNumber>
这是 Angular 4/5/6 中的工作示例:Limit Two decimal places number validation
希望这对你有帮助!!!
关于angular - 将输入字段限制为小数点后两位 - Angular 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50722368/
我试图找到一种方法来限制 Haskell,以便返回只有 2 个十进制名称的数字(例如 1.24) 我的代码是: sumSquares = sum . map (^ 2) 在 Haskell 中如何做到
我在向输入框添加停靠点验证时遇到一些问题。仅允许数字的限制(第二个条件)有效,但第一个条件也可能不存在。输入框中仍然没有出现句号和小数点。 function isNumberKey(evt) {
如何处理R中的p值? 我期望非常低的 p 值,例如: 1.00E-80 我需要-log10 -log10(1.00E-80) -log10(0) 是 Inf,但 Inf 也有四舍五入的感觉。 但似乎在
我的数据库中有一个包含货币字段的表。我已经为该货币字段创建了实体并创建了 decimal 属性。当该字段的值显示在我的 MVC3 View 上时,它在小数点后有四个零 0000,如下所示:5489.0
label.text = [NSString stringWithFormat:@"%.2f",final]; 上面的语句显示变量“final”中可用的浮点值,小数点后两位。 我想显示小数位数,具体取
有没有办法在 Qt 5 中绘制带有小数点大小的文本。我正在尝试使用 QFont::setPointSizeF() 但它似乎在我尝试过的任何平台(mac/linux/windows)上都不起作用,并且点
我有一个关于如何将基数为 10 的数字转换为 IEEE 754 浮点表示的示例 Number: 45.25 (base 10) = 101101.01 (base 2) Sign: 0 Normali
我有一个包含以下值的 CSV 文件: 3271.96;274;272;1;1;0;1;0.071690;0;0;0;0;0;0;1.753130;1.75;0;1.75; 但是当我用 Excel 打开
有人可以对 here 中的这些特殊字符示例给出更好的解释吗? ?或者提供一些更清楚的例子? (x) The '(foo)' and '(bar)' in the pattern /(foo) (bar
我编写了 change() 来帮助我将字符串转换为 double。 例如:如果字符串是“5.5”,我希望数字是5.5。如果字符串是“0.0”,我希望数字是0。另一个例子:“50”到50。 现在的问题是
我正在尝试确定 python float 与零进行比较时的精确度。请注意以下几点: 1e-323 == 0 > False 1e-324 == 0 > True 我认为阈值是 324 个小数点,至少对
我想使用 .ftr 文件来快速分析数百个表。不幸的是,我在十进制和千位分隔符方面遇到了一些问题,类似于 that post ,只是 read_feather 不允许 decimal=',', thou
在 SQL Server 2008 中,我正在运行一个报告,我需要在其中划分两列(称为 Completes 和 Prescreens,整数),然后转到 Excel并得到 Prescreens 除以 C
我有一个 NSString,我想将其转换为 NSDecimalNumber。该字符串是从服务器接收的,并且始终使用 en_US 语言环境进行格式化,例如 XXX.YYY 而不是 XXX,YYY。我想创
我是一名优秀的程序员,十分优秀!