Limiting the Decimal to 2 digits

23,706 views
Skip to first unread message

Dinesh P.R.

unread,
Aug 30, 2012, 2:15:41 PM8/30/12
to ang...@googlegroups.com
I am running a small expression in angularjs as shown in the code below

<span class="badge badge-success">{{((medicine.MRP-medicine.SRATE)*100)/medicine.SRATE}}  %</span>

I am trying to find the percentage of  profit in selling a medicine.
I want to limit the number of decimals in the percentage to 2.  Do we have any angular modules to limit the decimals?

Thanks 

Josh Kurz

unread,
Aug 30, 2012, 2:18:29 PM8/30/12
to ang...@googlegroups.com
You could write a filter that returns the substring 2 digits after the period in the double. 

Josh Kurz 

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 

Andy Joslin

unread,
Aug 30, 2012, 2:20:05 PM8/30/12
to ang...@googlegroups.com
Unfortunately, docs.angularjs.org is down..  but this works: https://github.com/angular/angular.js/blob/master/src/ng/filter/filters.js#L54

{{myNumber | number:2}} takes it to two decimals

Will Kriski

unread,
Aug 30, 2012, 2:25:31 PM8/30/12
to ang...@googlegroups.com

Dinesh P.R.

unread,
Aug 30, 2012, 11:09:25 PM8/30/12
to ang...@googlegroups.com
Thank you all, number filter worked very well

Christophe Levesque

unread,
Jun 22, 2013, 6:34:42 PM6/22/13
to ang...@googlegroups.com
Reviving and old thread but the number filter has an odd behavior. When not specifying the number of decimals {{myNumber | number}}, angular format with up to 3 decimals:
  • 123.4567 gets formatted as 123.457
  • 123 gets formatted as 123 (no decimals shown)
However, when specifying the number of decimals {{myNumber | number:2}}, angular forces them to appear even for integers:
  • 123.4567 gets formatted as 123.46
  • 123 gets formatted as 123.00
Is there a way to set the maximum number of decimals so that 123 would still appear as 123 without writing a custom filter?

Thanks,

Christophe

justinas...@visma.com

unread,
Apr 8, 2015, 3:03:55 AM4/8/15
to ang...@googlegroups.com
You can chain two number filters like this:
{{ myNumber | number:2 | number }}

Ciprian Serbu

unread,
Aug 8, 2016, 9:30:49 AM8/8/16
to AngularJS
actually chaining won't work if the number is greater or equal to 1000 as the first filter will add one or more commas and the number filter does not know how to parse numbers with commas, e.g. $filter('number')('1,000') returns an empty string

you could try calculating the number of digits yourself and passing it to the filter: $filter('number')(x, Math.min(String(x).split('.')[1] || 0, 2)) 

Lucas Lacroix

unread,
Aug 8, 2016, 9:41:07 AM8/8/16
to ang...@googlegroups.com
You could extend the pipe and make you're own. This is probably the best way to get the exact format you want.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+unsubscribe@googlegroups.com.

To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Lucas Lacroix
Computer Scientist
System Technology Division, MEDITECH
Reply all
Reply to author
Forward
0 new messages