Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a number in ng:pluralize

Tags:

angularjs

How to format a number passed into ng:pluralize directive via attribtute 'count'?

Consider following code:

<ng:pluralize count="5000000" when="{'other': '{} things'}"></pluralize>

the output is:

5000000 things

How can I modify this for output to be:

5,000,000 things    // in US locale
5 000 000 things    // in Czech locale

I tried using filter 'number', but I think I don't know where to put it. It doesn't work in the object passed to attribute 'when'. I tried these:

... when="{'many': '{{{}|number}} things'}"
... when="{'many': '{}|number things'}"
... when="{'many': '{|number} things'}"
like image 862
redhead Avatar asked Dec 22 '12 12:12

redhead


2 Answers

You need to assign the value to a variable

 <ng:pluralize ng-init="myCount=5000000" count="myCount" when="{'other': '{{myCount|number}} things'}"></ng:pluralize>

This will format the value to the current locale rules

Demo:

  • plunker
like image 107
Liviu T. Avatar answered Nov 12 '22 09:11

Liviu T.


Expanding on @Liviu T.'s answer, there's no real need to use ng-init to assign the variable. You can do it directly in count.

<ng:pluralize count="myCount=5000000" when="{'other': '{{myCount|number}} things'}"></ng:pluralize>
like image 33
Aaron S Avatar answered Nov 12 '22 09:11

Aaron S