Javascript 使用on双击事件时,AngularJS,$scope.amount不会显示为两位小数
我有两个Javascript 使用on双击事件时,AngularJS,$scope.amount不会显示为两位小数,javascript,angularjs,decimal,Javascript,Angularjs,Decimal,我有两个$scope对象$scope.grandTotal和$scope.amount,如果我双击$scope.amount的输入字段,它会自动用$scope.grandTotal中的值填充输入: <input type="text" placeholder="Enter Amount" ng-model="tenderedAmount" ng-change="updateBalance();" ng-dblclick="autoFill()" class="form-control" a
$scope
对象$scope.grandTotal
和$scope.amount
,如果我双击$scope.amount
的输入字段,它会自动用$scope.grandTotal
中的值填充输入:
<input type="text" placeholder="Enter Amount" ng-model="tenderedAmount" ng-change="updateBalance();" ng-dblclick="autoFill()" class="form-control" awnum num-sep="." num-int="15" num-thousand="true">
上面是函数autofill,它在双击事件期间调用。
现在是我的问题,比如说$scope.grandTotal
是1400.10,当我双击$scope.amount
的输入时,它填充为1400.1,我如何使它填充为1400.10?
我试过使用Math.round
和toFix(2)
,但两者都不起作用 Number(123)。toFixed(2)
返回123.00
这可能会有帮助。试试这个
$scope.tenderedAmount = (Math.round($scope.grandTotal * 100) / 100).toFixed(2);
并确保输入类型为数字而不是文本。试试看
parseFloat(Math.round(number * 100) / 100).toFixed(2)
var num1=parseFloat(数学四舍五入(100.1*100)/100.toFixed(2);
var num2=parseFloat(数学四舍五入(1000.1456*100)/100)。toFixed(2);
console.log(num1);
控制台日志(num2);
试试这个
角数过滤器
$scope.tenderedAmount = $filter('number')($scope.grandTotal, 2);
试试这个
$scope.tenderAmount=$scope.grandTotal.toFixed(2)
garndTotal值应该是一个数字
function MyCtrl($scope) {
$scope.grandTotal = 123.123;
$scope.tenderedAmount = 123.123;
$scope.autoFill = function(){
$scope.tenderedAmount = $scope.grandTotal.toFixed(2);
};
}
在控制器中,注入
$filter
并按如下方式使用编号
过滤器:
testModule.controller('testController', ['$scope', '$filter', function($scope, $filter) {
....
$scope.autoFill = function(){
var grandTotal = $scope.grandTotal;
$scope.tenderedAmount = $filter('number')(grandTotal, 2)
$scope.updateBalance();
};
....
}]);
希望这有帮助:)角度过滤器是最好的方法:
$scope.tenderedAmount = $filter('number')(grandTotal, 2)
您只需使用:
$filter('number')($scope.grandTotal, 2)
工作演示:
$filter('number')($scope.grandTotal, 2)
var myApp=angular.module('myApp',[]);
控制器('MyCtrl',函数($scope,$filter){
$scope.autoFill=函数(){
$scope.grandTotal=1400.10;
$scope.tenderAmount=$filter('number')($scope.grandTotal,2);
};
});代码>
你能为他们做一把js小提琴吗!感谢您的快速响应,它仍然不起作用:/。。问题是有两个输入,一个是禁用的,一个是文本输入(由于各种原因必须是文本),禁用的输入是$scope.grandTotal,另一个是$scope.tenderamount,tenderamount输入允许您在其中输入内容。因此,当我对其使用双击鼠标事件时,它会根据grandTotal自动填充tenderedamount。但是在我的例子中,如果它是.10,那么0总是丢失的,所以它会返回.1嘿,那么在注入$filter之后,你有没有收到任何错误?或者它仍然显示相同的内容而没有任何错误?当我使用$filter时没有给出错误,但它仍然返回xxx.1而不是xxx.10您是否尝试使用console.log($scope.tenderedAmount)
查看值?是的,在console.log中,它显示xxx.10,但在输入框中,它是xxx.1后面的0不存在。