Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rounding a number in input but keep full decimal value in model

Tags:

angularjs

I hope that title made sense. Basically I have an small app that does various social security calculations. A user can enter information like birth date, gender, salary etc, and click "calculate social security" and their monthly social security payouts display in an input field. The user can also, if they choose, enter that number in manually. The problem is that the value for that calculation is used elsewhere in the app, so for accuracy, i think I need the full decimal value. But cosmetically, i only need it to the dollar value (2046 vs 2046.3339228485938 bla bla bla). I've seen several tutorials on writing directives for that but that will change the value in the model. It's crossed my mind that i may be going about this the wrong way entirely but i'm turning to stackoverflow in the hopes that this is a common issue that I just cant seem to find the right words for to google.

thanks, John

like image 638
user1148613 Avatar asked Dec 07 '13 19:12

user1148613


People also ask

How do you store decimal value in access?

Press TAB, open the drop-down menu, and choose Number. Click in the Field Size property, open the drop-down menu, and choose Single. Press TAB, open the drop-down menu, and choose Fixed. Click in the Decimal Places property.

How do I limit the number of decimal places in HTML?

JavaScript Number toFixed() The toFixed() method converts a number to a string. The toFixed() method rounds the string to a specified number of decimals.

How do you only allow two numbers after a decimal in a text box?

Use JavaScript for validation input or use step=". 01" , which allows up to two decimal places on keypress input.

How do you input a decimal value in HTML?

You can also use a decimal value: for example, a step of 0.3 will allow values such as 0.3, 0.6, 0.9 etc, but not 1 or 2. Now you don't get a validation error. Yay! Also note that if you only want to accept positive numbers, you'll want to add min=”0″.


3 Answers

Yes, no need of custom filter for this purpose. Angular has its own filter to do this job. Try this one

<div ng-controller="Ctrl">{{dollar | number:2}}</div>
like image 158
Munawer Aziz Avatar answered Sep 24 '22 13:09

Munawer Aziz


You can create specific filter, something like:

var app = angular.module("MyApp",[]);

app.filter("rounded",function(){    
    return function(val,to){
        return val.toFixed(to || 0);
    }
});

function Ctrl($scope){
    $scope.dollar=2046.3339228485938;
}

And use it like:

<div ng-controller="Ctrl">{{ dollar | rounded:2 }}</div>

Example: http://jsfiddle.net/RdgR2/

like image 34
Ivan Chernykh Avatar answered Sep 23 '22 13:09

Ivan Chernykh


The other answers aren't quite correct for this because the OP wants it on an input field, but the rounding to be display only.

Using parsers and formatters is a working approach. I found this snippet (https://www.snip2code.com/Snippet/43223/angular-directive-to-format-a-number-in-) and modified it.

Working fiddle is here: http://jsfiddle.net/2akaxojg/

.directive('inputCurrency', function ($filter, $locale) {
    return {
        terminal: true,
        restrict: 'A',        
        require: '?ngModel',
        link: function (scope, element, attrs, ngModel) {

            if (!ngModel)
                return; // do nothing if no ng-model

            // get the number format
            var formats = $locale.NUMBER_FORMATS;

            // fix up the incoming number to make sure it will parse into a number correctly
            var fixNumber = function (number) {
                if (number) {
                    if (typeof number !== 'number') {
                        number = number.replace(',', '');
                        number = parseFloat(number);
                    }
                }
                return number;
            }

            // function to do the rounding
            var roundMe = function (number) {
                number = fixNumber(number);
                if (number) {
                    return $filter('number')(number, 2);
                }
            }

            // Listen for change events to enable binding
            element.bind('blur', function () {                                
                element.val(roundMe(ngModel.$modelValue));
            });          

            // push a formatter so the model knows how to render
            ngModel.$formatters.push(function (value) {
                if (value) {
                    return roundMe(value);
                }
            });

            // push a parser to remove any special rendering and make sure the inputted number is rounded
            ngModel.$parsers.push(function (value) {
                return value;
            });
        }
    };
});
like image 45
LameCoder Avatar answered Sep 23 '22 13:09

LameCoder