最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to restrict decimal upto 2 digits only in angularjs - Stack Overflow

programmeradmin0浏览0评论

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>

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>
Share Improve this question asked Mar 8, 2016 at 8:11 Krishna MohanKrishna Mohan 3151 gold badge7 silver badges15 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Please see the angularjs example

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

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

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

You can try javascript toFixed method like below

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

you can try

<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>
发布评论

评论列表(0)

  1. 暂无评论