1

I want to write a filter in angularjs that separates thousands by whitespaces for example

(1000 -> 1 000) and if (1000.555 -> 1 000.555).

Now I am trying to do the next but it seems does not work:

app.filter('split', function () {
    return function (number, number2) {
        if (number/3==1){
            return number;
        }else{

            return number.split(".")+"."+ number2.split('.')

        }
    }
});

and for my input I put the next <input type="text" {{someExpression|split}} >

Dr. Abbos
  • 139
  • 2
  • 9
  • 1
    FYI: after this operation your number isn't a number anymore. You will return an string (just to remind if calculating afterwards with it). – zypro Sep 20 '17 at 05:54

2 Answers2

0

Maybe this answer will help you to solve your problem too. (then maybe duplicate?)

app.filter('split', function (x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
});
zypro
  • 1,158
  • 3
  • 12
  • 33
  • I have tested it in console, it does not work: Uncaught SyntaxError: Invalid or unexpected token, for x I put 10000 – Dr. Abbos Sep 20 '17 at 05:59
  • hmm it works for me. I update my answer. Maybe you have to apapt the filter. Don't copy paste the function just like that ;-) – zypro Sep 20 '17 at 06:04
0

Try this:

app.filter('split', function () {
    return function (number) {
        number.toLocaleString();
    }
});
Faly
  • 13,291
  • 2
  • 19
  • 37