Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoundUp - AngularJS

Tags:

angularjs

I am using the following code to try and roundup in Angular, which on the whole works, however numbers less than 0.5 are rounded to 0. I would like to round up every number to the next whole number. E.g 0.02 should be rounded to 1

{{((data.Virtual.SumCores/data.Physical.SumCores) | number:0)*1}}

like image 657
CMS Avatar asked Sep 18 '15 10:09

CMS


People also ask

How do you round off in AngularJS?

The Math. round() method rounds a number to the nearest integer. 2.49 will be rounded down (2), and 2.5 will be rounded up (3).

How to round Up in JavaScript?

ceil() The Math. ceil() function always rounds up and returns the smaller integer greater than or equal to a given number.

Why use AngularJS?

AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. AngularJS's data binding and dependency injection eliminate much of the code you would otherwise have to write.


3 Answers

Use this

Math.ceil(4.4); //Returns 5
Math.ceil(0.002); //Returns 1

The filter

 .filter('roundup', function () {
        return function (value) {
            return Math.ceil(value);
        };
    })

The HTML

{{ yourdata | roundup }}

reference

JS Math

like image 55
stackg91 Avatar answered Oct 18 '22 22:10

stackg91


You can create a filter for this:

.filter('roundup', function() {
  return function(input) {
    return Math.ceil(input);
  };
});

And use it like this:

{{ data.Virtual.SumCores/data.Physical.SumCores | roundup }}
like image 44
Razvan B. Avatar answered Oct 18 '22 22:10

Razvan B.


Since all of the answers are based on Math.ceil() function, you can simply add

$scope.Math=window.Math;

into your controller and use it like this:

{{ Math.ceil(data.Virtual.SumCores/data.Physical.SumCores) }}

This way you get the access to all Math functions.

like image 2
Davor Avatar answered Oct 18 '22 20:10

Davor