使用angularjs将数字粘贴到输入框中时,如何将小数位数限制为两位?
我有一个输入框,可以接受小数点最多不超过2位的十进制数字。此外,我不允许输入超过两位小数 例如9999.99 但现在的问题是,当我复制一些数字,即9999999.9999,并将其粘贴到输入框中时,我希望得到结果,即9999999.99 这意味着我想截断其余的小数位,也不想四舍五入使用angularjs将数字粘贴到输入框中时,如何将小数位数限制为两位?,angularjs,Angularjs,我有一个输入框,可以接受小数点最多不超过2位的十进制数字。此外,我不允许输入超过两位小数 例如9999.99 但现在的问题是,当我复制一些数字,即9999999.9999,并将其粘贴到输入框中时,我希望得到结果,即9999999.99 这意味着我想截断其余的小数位,也不想四舍五入 如何使用angularjs实现这一点?您可以使用ngPattern指令 <input type="text" ng-pattern="/^[0-9]+(?:\.[0-9][0-9])?$/"> 这个正
如何使用angularjs实现这一点?您可以使用ngPattern指令
<input type="text" ng-pattern="/^[0-9]+(?:\.[0-9][0-9])?$/">
这个正则表达式将强制执行两个位置(不多或少)。只需更改正则表达式即可获得所需的行为。您可以使用ngPattern指令
<input type="text" ng-pattern="/^[0-9]+(?:\.[0-9][0-9])?$/">
这个正则表达式将强制执行两个位置(不多或少)。只需更改正则表达式即可获得所需的行为。您可以使用
ng model
以及$formatters
和$parsers
来实现这一点
请参见此处的更多说明:
这是一个TypeScript改编自一些我用来强制货币格式的代码
app.directive('myModelFormat', ['$filter', function myModelFormatDirective($filter:ng.IFilterService):ng.IDirective {
return {
require: '?ngModel',
link: myModelFormatLink
};
function myModelFormatLink(scope:ng.IScope, elem:ng.IAugmentedJQuery, attrs:ng.IAttributes, ctrl:ng.INgModelController) {
if (!ctrl) {
return;
}
const decimalWithOneDigit:RegExp = new RegExp('\\' + STRINGS.decimalSym + '\\d$');
ctrl.$formatters.unshift(function myModelFormatFormatter(ignore:any):string {
return $filter(attrs['myModelFormat'])(ctrl.$modelValue)
});
ctrl.$parsers.unshift(function myModelFormatParser(viewValue:string):any {
const plainNumber:number = parseFloat(viewValue);
let formatted:string = $filter(attrs['myModelFormat'])(plainNumber);
// special case where they typed the decimal symbol but not a decimal value,
// make sure we don't remove the symbol which prevents them from typing a decimal
if (_s.endsWith(viewValue, STRINGS.decimalSym)) {
formatted += STRINGS.decimalSym;
}
// alternate special case, they typed one decimal number, don't force it to zero, at least not yet..
if (decimalWithOneDigit.test(viewValue)) {
formatted = formatted.substr(0, formatted.length - 1);
}
elem.val(formatted);
return plainNumber;
});
}
}]);
您可以使用
ng model
以及$formatters
和$parsers
来实现这一点
请参见此处的更多说明:
这是一个TypeScript改编自一些我用来强制货币格式的代码
app.directive('myModelFormat', ['$filter', function myModelFormatDirective($filter:ng.IFilterService):ng.IDirective {
return {
require: '?ngModel',
link: myModelFormatLink
};
function myModelFormatLink(scope:ng.IScope, elem:ng.IAugmentedJQuery, attrs:ng.IAttributes, ctrl:ng.INgModelController) {
if (!ctrl) {
return;
}
const decimalWithOneDigit:RegExp = new RegExp('\\' + STRINGS.decimalSym + '\\d$');
ctrl.$formatters.unshift(function myModelFormatFormatter(ignore:any):string {
return $filter(attrs['myModelFormat'])(ctrl.$modelValue)
});
ctrl.$parsers.unshift(function myModelFormatParser(viewValue:string):any {
const plainNumber:number = parseFloat(viewValue);
let formatted:string = $filter(attrs['myModelFormat'])(plainNumber);
// special case where they typed the decimal symbol but not a decimal value,
// make sure we don't remove the symbol which prevents them from typing a decimal
if (_s.endsWith(viewValue, STRINGS.decimalSym)) {
formatted += STRINGS.decimalSym;
}
// alternate special case, they typed one decimal number, don't force it to zero, at least not yet..
if (decimalWithOneDigit.test(viewValue)) {
formatted = formatted.substr(0, formatted.length - 1);
}
elem.val(formatted);
return plainNumber;
});
}
}]);
如果我复制一些数字,例如9999.999并将其粘贴到输入框中,此代码是否有效?它是否会将其转换为9999.99?@NilayMistry,是的,格式正确应用于粘贴的值。如果我复制一些数字,例如9999.999并将其粘贴到输入框中,此代码是否有效?它是否会将其转换为9999.99?@NilayMistry,是的,格式正确地应用于粘贴的值。我关心的是,当我将数字粘贴到输入框中时,数字没有被格式化。模式将为您提供一个真实的值,即框中当前的文本是否符合正则表达式。你可以随意处理。请参阅:我关心的是,当我将数字粘贴到输入框中时,数字不会被格式化。模式只会为您提供一个真实值,即框中当前的文本是否符合正则表达式。你可以随意处理。见: