Skip to content

Commit a59c9f2

Browse files
committed
1 parent f14c3c7 commit a59c9f2

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

demo.html

+7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ <h3>limitFrom(1,2)</h3>
5454

5555
<h3>wordcount</h3>
5656
{{'Hello World' | wordcount}}
57+
58+
<h3>round</h3>
59+
{{ 45.35 | round }}
60+
61+
<h3>round(1, 'floor')</h3>
62+
{{ 45.35 | round(1, 'floor') }}
63+
5764
</template>
5865
<script>
5966
var template = document.getElementById('view');

filter-round.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Round a number by a specific precision or method
3+
* @param {integer} val
4+
* @param {integer} precision
5+
* @param {string} method
6+
* @return {string}
7+
*/
8+
PolymerExpressions.prototype.round = function (val, precision, method) {
9+
precision = precision || 0;
10+
var factor = Math.pow(10, precision);
11+
var rounder;
12+
13+
if (method == 'ceil') {
14+
rounder = Math.ceil;
15+
} else if (method == 'floor') {
16+
rounder = Math.floor;
17+
} else {
18+
rounder = Math.round;
19+
}
20+
21+
return rounder(val * factor) / factor;
22+
};

polymer-filters.html

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<script src="filter-replace.js"></script>
99
<script src="filter-startsWith.js"></script>
1010
<script src="filter-wordcount.js"></script>
11+
<script src="filter-round.js"></script>
1112

1213
<!--
1314
A collection of Polymer filters for formatting values of expressions for display to users
@@ -56,6 +57,12 @@
5657
5758
{{'Hello World' | wordcount}}
5859
60+
####Rounding
61+
62+
{{ 45.35 | round }}
63+
64+
{{ 45.35 | round(1, 'floor') }}
65+
5966
@element polymer-filters
6067
@blurb A collection of Polymer filters for formatting values of expressions for display to users
6168
@homepage http://addyosmani.github.io/polymer-filters

0 commit comments

Comments
 (0)