2017-11-07 1 views
1

Gibt es eine eingebaute Möglichkeit, eine Zahl nach unten zu runden, unabhängig von den Dezimalstellen?Anzahl Abrundung in AngularJS

Zum Beispiel

<div>{{var/3 | number:0}}</div> 

, wenn das Ergebnis der Division ist 9.99 Ich möchte nur 9 zeigen, nicht mehr als 10

Dank

Antwort

2

Verwenden Sie einen benutzerdefinierten Filter, hier ist eine große Antwort von: http://tianes.logdown.com/posts/2015/12/08/rounding-a-number-to-the-nearest-neighbour-up-and-down

/********************************************************* 
    - Author: Sebastian Cubillos 
    - Github: @tianes 
    - More Gists: https://gist.github.com/tianes/ 
    - Contact: [email protected] 
    - Article: http://tianes.logdown.com/posts/2015/12/08/rounding-a-number-to-the-nearest-neighbour-up-and-down 
**********************************************************/ 

app.filter('round', function() { 
    /* Use this $filter to round Numbers UP, DOWN and to his nearest neighbour. 
     You can also use multiples */ 

    /* Usage Examples: 
     - Round Nearest: {{ 4.4 | round }} // result is 4 
     - Round Up: {{ 4.4 | round:'':'up' }} // result is 5 
     - Round Down: {{ 4.6 | round:'':'down' }} // result is 4 
     ** Multiples 
     - Round by multiples of 10 {{ 5 | round:10 }} // result is 10 
     - Round UP by multiples of 10 {{ 4 | round:10:'up' }} // result is 10 
     - Round DOWN by multiples of 10 {{ 6 | round:10:'down' }} // result is 0 
    */ 
    return function (value, mult, dir) { 
     dir = dir || 'nearest'; 
     mult = mult || 1; 
     value = !value ? 0 : Number(value); 
     if (dir === 'up') { 
      return Math.ceil(value/mult) * mult; 
     } else if (dir === 'down') { 
      return Math.floor(value/mult) * mult; 
     } else { 
      return Math.round(value/mult) * mult; 
     } 
    }; 
}); 
1

Sie Javascripts Boden nutzen könnten() denen rund eine Zahl nach unten zur nächsten ganzen Zahl

var numb = Math.floor(9.99) // numb = 9 
+0

scheint, wie aus der Syntax und die Tags, die OP, um herauszufinden will, wie dies als mit Vanille JavaScript in AngularJS eher zu tun. – DrCord

Verwandte Themen