techwestcoastsfosea
techwestcoastsfosea

Reputation: 778

Angularjs / Ionic always show 2 decimal places

I have a an input form with the following html

Which prints price as {{item.myvalue}}

if I enter the price as 2.15 it prints as 2.15. However, if I enter it as 2.00 it prints as 2. I would like to print it as 2.00. How do I go about it?

Upvotes: 2

Views: 2951

Answers (2)

Arun Ghosh
Arun Ghosh

Reputation: 7754

You can do it using a custom filter:

angular.module('app')
.filter('fixTwoDecimal', function() {
    return function(num) {
        if(!num) return;
        var result = String(num);
        if(result.indexOf('.') === -1) result = result + '.00';
        else {
            var parts = result.split('.');
            if(parts[1].length === 1) result = result + '0';
        }
        return result;
    }
});

Upvotes: 0

Marek Woźniak
Marek Woźniak

Reputation: 1786

Use number filter:

{{item.myvalue | number:2}}

Upvotes: 5

Related Questions