2

I am applying an AngularJS filter programmatically like so:

filtered = $filter('number')(value, 2);

This will give me something like 5,000.00. I need a way to take the filtered value and strip out the formatting so I can get 5000 from 5,000.00. I can do this with javascript but thought there might be a way to do this using Angular that I am not familiar with.

Thoughts?

4
  • 2
    uses parseFloat, but before you need to remove the comma: parseFloat(filtered.replace(/,/, '')) Commented Sep 14, 2016 at 13:41
  • why not leave the value as number and only use number filter in the view for display only? Commented Sep 14, 2016 at 13:49
  • @Dustin If you're ok with our answers, please accept one, or provide some feedback. Thanks Commented Sep 14, 2016 at 15:10
  • My question must not be very clear - no one is answering it. "I can do this with javascript but thought there might be a way to do this using Angular that I am not familiar with". Basically, yes everyone, I know HOW to do what I am asking using javascript. I thought there might be mechanism in Angular to "unfilter" a string. I used the number filter as an example, but I need to do this using multiple string filters. Commented Sep 14, 2016 at 16:19

3 Answers 3

1

Anything you can do in vanilla js or jQuery, you can do it in AngularJS, most of the times. So yes you can convert float/double into int in AngularJS, like this

in controller :

$scope.floatNum1 = 12.00
$scope.floatNum2 = 10.89

In View :

{{floatNum1 | number:0}}  // it will render 12
{{floatNum2 | number:0}}  //it will render 11

Hope this is what you were looking for

Sign up to request clarification or add additional context in comments.

Comments

1

How about to create own custom filter?

app.filter('round', function() {
  return function(input, count) {
    var output;

    //Your code here
    output = Math.floor(input, count); //something like that
    return output;

  }
});

and

filtered = $filter('round')(value, 2);

or in html:

<span ng-bind="myVal | round:2"></span>  <!-- or <span> {{myVal | round}} </span> -->

Here is a jsFiddle

Comments

0

Just parse as float without commas:

var filtered = '5,000,000.00';
var source = parseFloat(filtered.replace(/,/g, ''));

I used Joaozito Polo's answer but added g modifier to the RegExp because without it it will replace only 1 comma.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.