I am trying to make a currency/number input/output without rounding.
The issue I come across with using currency is two-fold, (1) it rounds the second decimal once a third number has been entered, (2) that it even allows a third number to be entered. If you notice my del() function, it deletes the end of the number, but while the display might be: $27.46. The string could actually be 27.45606020, and backspacing would delete numbers the user can't even see.
At the moment I have some hacky code that doesn't even bother with AngularJS currency or number and uses a filter to prevent digits after two decimal places, as well when a decimal is added, I have it so it can only be added once.
{{checkTotal | dropDigits}
.filter('dropDigits', function() {
return function(floatNum) {
return String(floatNum)
.split('.')
.map(function (d, i) { return i ? d.substr(0, 2) : d; })
.join('.');
};
})
.
.controller('tipController', function($scope) {
// Numpad
$scope.checkTotal = '0.00';
$scope.clicked = function (label) {
if($scope.checkTotal === '0.00') {
$scope.checkTotal = label;
} else {
$scope.checkTotal += label;
}
};
// Prevent multiple decimals
$scope.clickedDot = function() {
if (($scope.checkTotal.indexOf('.') < 0) || ($scope.checkTotal === '0.00')) {
if (($scope.checkTotal === '0.00') || ($scope.checkTotal === '')) {
$scope.checkTotal = '0.';
} else {
$scope.checkTotal += '.';
}
}
};
$scope.del = function () {
$scope.checkTotal = $scope.checkTotal.slice(0, -1);
};
});