简体   繁体   中英

AngularJS: Change decimal separator to comma

I hope I get a solution for this. I am using angular1.4 .

I want to display time in decimal numbers(separated by comma because users are in Germany-Europe).

For example 5min is expected to be displayed as:

5min=(5/60)=0,08 (2 decimal places)

Then I came across this solution: AngularJS number filter . However the result appears in decimal, what can I add to this pipe filter below, to change/replace decimal to comma?

<span>{{ dr.duration/(60*60*1000) | number:2}}</span>

In case you need clarity lemme know.

Check out the documentation for the $locale service.

Here 'sa list of locales in the source code.

The example that they use for supported locales is actually for Germany :~).

Btw, In the AngularJS docs there's a button at the top of each page that says [view source code] at the top of each page.

To change the separators used by the AngularJS number filter, include the appropriate locale rule set such as angular-locale_de-de.js .

There are two approaches to providing locale rules to AngularJS:

For more information, see

The DEMO

 <script src="//unpkg.com/angular/angular.js"></script> <script src="//unpkg.com/angular-i18n/angular-locale_de-de.js"></script> <body ng-app> <span><b>5min=(5/60)=</b> {{ 5/60 | number:2 }} hr (2 decimal places) </span> </body>

So I found another way to do it without locale service. I added this pipe

app.filter('timeToDecimals', ($filter) => {
    return function (input) {
      let time = Number(input);
        if (!Number.isNaN(time)) {
            let number = time/(60*60*1000);
            return $filter('number')(number, 2).replace(".",",");
        } 
        else return '00,00';
    }
});

Where the input is from duration.

I hope this helps someone out there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM