AngularJS currency filter: can I remove the .00 if there are no cents in the amount?

后端 未结 3 871
闹比i
闹比i 2021-02-20 09:32

I am following this: http://docs.angularjs.org/api/ng.filter:currency When I type 1234.56 in the field then the output should be $1,234.56. But if I type in the input 1234 then

3条回答
  •  Happy的楠姐
    2021-02-20 10:13

    If you want something quick and dirty, you can set the second parameter to the filter as such in order to trigger two decimal places when there are, in fact, cents to be shown:

    {{amount | currency:undefined:2*(amount % 1 !== 0)}}
    

    Quick caveat is that this only works roughly up to 10^14 since a loss of floating point precision will cause amount % 1 to return 0.

    An alternative syntax, which works pretty much identically but is a little less understandable is 2*!!(amount % 1)

提交回复
热议问题