Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number filter filters number down to two decimals, but does not display it that way

This is what I see:

The value is set at 160.90, but displays as 160.8999999999 etc.

<input class="form-control" ng-model="imprint.total" 
    value="{{imprint.total | number:2}}" readonly>

It goes through filtering of certain inputs to get that total, but essentially it's just a price multiplied to quantity.

like image 493
swade Avatar asked Jun 10 '15 15:06

swade


People also ask

How do you check if a number has a decimal in Excel?

Count and identify the decimal place of a number You can do it with following steps: In a blank cell, saying the Cell B2, enter the formula of =IF(A2=INT(A2),0,LEN(MID(A2-INT(A2),FIND(".",A2,1),LEN(A2)-FIND(".",A2,1)))), and press the Enter key. Then it returns the decimal place in the Cell B2.

How do I get decimal places in Excel?

The TRUNC function returns the integer portion of the number which is then subtracted from the original value. The result is the decimal portion of the number. If the original number is an integer to begin with, the result of this formula is zero. With TRUNC, no rounding takes place.


1 Answers

The value in the value attribute will be overridden by ng-model directive when it sets the viewvalue as it renders. You have a readonly textbox you could just as well remove the ng-model from it.

<input class="form-control" 
       value="{{imprint.total | number:2}}" readonly>

With ng-model and to format live data entry you would need to create a directive and use parsers/formatters to format the value.

angular.module('app', []).directive('format', ['numberFilter',
  function(numberFilter) {
    return {
      restrict: 'A',
      require: 'ngModel',
      link: function(scope, elm, attr, ngModel) {
        var decPlaces = attr.format || 2;

        function formatter(value) {

          if (value) {
            return numberFilter(value, decPlaces);
          }
        }

        ngModel.$formatters.push(formatter);

      }
    }
  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-init="imprint=164.899999">
  <input class="form-control" value="{{imprint | number:2}}" readonly>

  <input class="form-control" ng-model="imprint" format="2" readonly>

</div>
like image 130
PSL Avatar answered Nov 15 '22 04:11

PSL