Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs : format number according to locale

Tags:

angularjs

I need to display this number 2387,15
I am doing this with angularjs :

{{myNumber|number:2}}

I am getting 2,387.15 I dont want a coma betwen 2 and 3 (I am french).

Is there a way to set the locale with angularjs?

Thanks

like image 363
user1260928 Avatar asked Sep 02 '15 14:09

user1260928


Video Answer


1 Answers

Approach #1. If you want french locale then you can simply include corresponding localization file. For example:

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-i18n/1.4.5/angular-locale_fr-fr.js"></script>

Then Angular will pick it up and use for all localization.

<script src="https://code.angularjs.org/1.4.3/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-i18n/1.4.5/angular-locale_fr-fr.js"></script>
<script>
  angular.module('demo', []).controller('mainCtrl', function($scope) {
    $scope.myNumber = 2387,15;
  });
</script>

<div ng-app="demo" ng-controller="mainCtrl">
  {{myNumber|number:2}}
</div>

Approach #2. Another simple thing you can do is to customize NUMBER_FORMATS.GROUP_SEP constant:

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

<div ng-app="demo" ng-controller="mainCtrl">
    {{myNumber|number:2}}    
</div>
like image 81
dfsq Avatar answered Oct 03 '22 05:10

dfsq