9

How to change delimiter in Angularjs number filter from comma to something custom?

Now {{ price | number }} returns 1,000.00. And I need to have it like 1 000.00.

How exactly do I need to rewrite built-in angular filter?

1 Answer 1

16

You don't have to mess with Angular source or change locale to other. You don't even need to write custom filter for this. just change NUMBER_FORMATS.GROUP_SEP of the $locale service to whatever you need:

$locale.NUMBER_FORMATS.GROUP_SEP = ' ';
$scope.price = 100000;

But it's better to do it in run block, rather then in controller.

angular.module('demo', []).controller('MainCtrl', function($scope, $locale) {
    $locale.NUMBER_FORMATS.GROUP_SEP = ' ';
    $scope.price = 100000;
});
<script src="https://code.angularjs.org/1.4.3/angular.js"></script>

<div ng-app="demo" ng-controller="MainCtrl">
    {{ price | number }}    
</div>

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

3 Comments

Are you sure in run block? Not in config one?
Anyway, it works. I guess, the best answer. Thanks dude.
Because $localeProvider doesn't expose those properties, so you can't reset it so easily in config block. Better to say: because ngLocale module creates $locale which is a value (not provider or constant), so it's not accessible in config block.

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.