简体   繁体   中英

how to restrict decimal upto 2 digits only in angularjs

im doing some math operations using angular js . while calculating percentage/vat price showing - 0.35000000000000003 like this . i need 0.35.

0.35000000000000003. how to trim decimals after 2 digits in .

 <table align="center">

            <h2>Tax Calculations</h2>

            <tr>
                <td>
                     quantity : 
                </td>
                <td>
                 <asp:TextBox runat="server" ng-model="qty"/>
                </td>
            </tr>

              <tr>
                <td>
                     unit Price : 
                </td>
                <td>
                 <asp:TextBox runat="server" ng-model="price"/>
                </td>
            </tr>
               <tr>
                <td>
                     total Amount  : 
                </td>
                <td>
                 <asp:TextBox runat="server" Text='{{qty * price}}' ReadOnly="true"/>
                </td>
               </tr>
            <tr>
                <td>
                     Vat Price at 5%  : 
                </td>
                <td>
                 <asp:TextBox runat="server" Text='{{(qty * price )/100  * 5}}' ReadOnly="true"/>
                </td>
               </tr>
             <tr>
                <td>
                     Total With Tax  : 
                </td>
                <td>
                 <asp:TextBox runat="server" Text='{{((qty * price )/100  * 5) + (qty*price)}}' ReadOnly="true"/>
                </td>
               </tr>
        </table>

Please see the angularjs example

https://docs.angularjs.org/api/ng/filter/number

By the way ,suggest not mix asp.net and angularjs code into the same tag

 //number with two digit
 Negative number: <span>{{-val | number:2}}</span>

您可以尝试javascript toFixed方法,如下所示

{{((qty * price )/100  * 5).toFixed(2)}}

你可以试试

<asp:TextBox runat="server" Text='{{(qty * price )/100  * 5 | number:2}}' ReadOnly="true"/>

angularjs document contains a very simple and pretty example. You can do this by :

<script>
  angular.module('numberFilterExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.val = 1234.56789;
    }]);
</script>
<div ng-controller="ExampleController">
  <label>Enter number: <input ng-model='val'></label><br>
  Default formatting: <span id='number-default'>{{val | number}}</span><br>
  No fractions: <span>{{val | number:0}}</span><br>
  Negative number: <span>{{-val | number:4}}</span>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM