Fixed a issue with precision when using floats/decimals Added focus as a default event to bind to
jdarwood007

jdarwood007 commited on 2021-06-11 14:18:00
Showing 1 changed files, with 29 additions and 5 deletions.

... ...
@@ -57,7 +57,7 @@
57 57
 			inputParentCss: 'input-group numberControl',
58 58
 			inputParent: 'div',
59 59
 			inputCss: 'numberControlInput form-control px-1',
60
-			bindButtonEvents: 'click tap touch touchstart',
60
+			bindButtonEvents: 'click tap touch touchstart focus',
61 61
 			keyboardLanguage: {
62 62
 				'UP' : '<span class="oi oi-chevron-top" />',
63 63
 				'DOWN' : '<span class="oi oi-chevron-bottom" />',
... ...
@@ -86,6 +86,29 @@
86 86
 				options.onAfterSetNewvalue(this, event, container, value);
87 87
 		}
88 88
 
89
+		// https://stackoverflow.com/questions/9553354/how-do-i-get-the-decimal-places-of-a-floating-point-number-in-javascript
90
+		function precision(a)
91
+		{
92
+			if (!isFinite(a))
93
+				return 0;
94
+
95
+			var e = 1, p = 0;
96
+			while (Math.round(a * e) / e !== a)
97
+			{
98
+				e *= 10;
99
+				p++;
100
+			}
101
+
102
+			return parseInt(p);
103
+		}
104
+
105
+		function FindPercision(v, p)
106
+		{
107
+			if (!isFinite(v))
108
+				return 0;
109
+			return parseInt(v).toString().length + p;
110
+		}
111
+
89 112
 		function findMinMaxValue()
90 113
 		{
91 114
 			var testValue;
... ...
@@ -112,6 +135,7 @@
112 135
 
113 136
 			// Some settings we either can pull in from a options, from standard attributes or defaults.
114 137
 			var $step = findMinMaxValue(parseFloat(options.step), parseFloat($base.attr('step')), 1);
138
+			var $percision = precision($step) + 1;
115 139
 			var $minValue = findMinMaxValue(options.min, $base.attr('min'), Number.MIN_VALUE);
116 140
 			var $maxValue = findMinMaxValue(options.max, $base.attr('max'), Number.MAX_VALUE);
117 141
 
... ...
@@ -146,11 +170,11 @@
146 170
 				if (options.onBeforeClickDecrease !== undefined)
147 171
 					options.onBeforeClickDecrease(this, event);
148 172
 				if ($base.val() > $minValue)
149
-					setNewValue($base, parseFloat($base.val()) - parseFloat($step));
173
+					setNewValue($base, parseFloat(parseFloat($base.val()) - parseFloat($step)).toPrecision(FindPercision($base.val(), $percision)));
150 174
 				if (options.onAfterClickDecrease !== undefined)
151 175
 					options.onAfterClickDecrease(this, event);
152 176
 				if (options.debug)
153
-					console.log('numbercontrl: decreaseButton: Click', event, $base.val(), $minValue);
177
+					console.log('numbercontrl: decreaseButton: Click', event, parseFloat($base.val()), $minValue, parseFloat($step));
154 178
 			});
155 179
 
156 180
 			// The increase event.
... ...
@@ -161,11 +185,11 @@
161 185
 				if (options.onBeforeClickIncrease !== undefined)
162 186
 					options.onBeforeClickIncrease(this, event);
163 187
 				if ($base.val() < $maxValue)
164
-					setNewValue($base, parseFloat($base.val()) + parseFloat($step));
188
+					setNewValue($base, parseFloat(parseFloat($base.val()) + parseFloat($step)).toPrecision(FindPercision($base.val(), $percision)));
165 189
 				if (options.onAfterClickIncrease !== undefined)
166 190
 					options.onAfterClickIncrease(this, event);
167 191
 				if (options.debug)
168
-					console.log('numbercontrl: increaseButton: Click', event, $base.val(), $minValue);
192
+					console.log('numbercontrl: increaseButton: Click', event, parseFloat($base.val()), $maxValue, parseFloat($step));
169 193
 			});
170 194
 
171 195
 			// The Popup Numberpad
172 196