-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal-behavior.html
85 lines (84 loc) · 3.17 KB
/
decimal-behavior.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<link rel="import" href="../polymer/polymer.html">
<script>
/**
A decimal-behavior element that provides ceil, round, floor, format and decimalAdjustemnt methods.
Decimal adjustment of a number implementation is taken from Mozilla Developer Network:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round.
@demo demo/index.html
@polymerBehavior DecimalBehavior
*/
DecimalBehavior = {
/**
* Adjusts and formats the passed value to the specified number of decimals using the passed adjustment type.
* The toFixed function is used to cut off the String representation.
* @param {Number} value The number.
* @param {Integer} decimals The number of decimals.
* @param {String} type round, floor or ceil adjustment type. round is set as default.
* @returns {String} The adjusted and formatted String representation of the passed value.
*/
formatNumber: function(value, decimals, type) {
if (value === null || value === undefined) {
return '';
}
if (!type) {
type = 'round';
}
var roundedValue = this.decimalAdjust(type, value, decimals * -1);
return roundedValue.toFixed(decimals);
},
/**
* Decimal adjustment of a number using the round method.
* @param {Number} value The number.
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
* @returns {Number} The adjusted value.
*/
round: function(value, exp) {
return this.decimalAdjust('round', value, exp);
},
/**
* Decimal adjustment of a number using the floor method.
* @param {Number} value The number.
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
* @returns {Number} The adjusted value.
*/
floor: function(value, exp) {
return this.decimalAdjust('floor', value, exp);
},
/**
* Decimal adjustment of a number using the ceil method.
* @param {Number} value The number.
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
* @returns {Number} The adjusted value.
*/
ceil: function(value, exp) {
return this.decimalAdjust('ceil', value, exp);
},
/**
* Decimal adjustment of a number.
* Copyright by Mozilla Developer Network: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
*
* @param {String} type The type of adjustment.
* @param {Number} value The number.
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
* @returns {Number} The adjusted value.
*/
decimalAdjust: function(type, value, exp) {
// If the exp is undefined or zero...
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
// If the value is not a number or the exp is not an integer...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
// Shift
value = value.toString().split('e');
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
// Shift back
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}
};
</script>