I have noticed some not so nice behaviour of angularjs when using Float32Array values.
Essentially I tested these three
angular.module("myApp", []).controller("myCtrl", function($scope) {
$scope.n = 0.2; // Displays as 0.2
$scope.arr1 = new Float32Array(1);
$scope.arr1[0] = 0.2; // 0,20000000298023224
$scope.arr2 = new Float64Array(1);
$scope.arr2[0] = 0.2; // Displays as 0.2
});
I do understand the problem with 0.2 being periodical as a binary fraction.
However, do you have any idea how to tell the toString method (or some other function) to take account for the lower precision and round the decimal when appropriate?
This is the directive that I added to make it a bit better (but still not quite):
.directive("numberfloat", function(){
return {
scope: {n: "=model"},
template: '<input type="number" ng-model="m" ng-model-options="{getterSetter: true}"/>',
link: function($scope){
$scope.m = function(newVal){
if(newVal){
$scope.n = newVal;
}
return parseFloat($scope.n.toFixed(4));
}
}
}
})
Aucun commentaire:
Enregistrer un commentaire