mirror of
https://github.com/jquery/jquery-ui.git
synced 2024-10-05 01:44:18 +00:00
Spinner: Fixed precision when stepping.
Thanks hughlomas
This commit is contained in:
parent
6c1bf56029
commit
257021b1af
@ -185,4 +185,21 @@ test( "focus text field when pressing button", function() {
|
||||
ok( element[ 0 ] === document.activeElement, "focused after" );
|
||||
});
|
||||
|
||||
test( "precision", function() {
|
||||
expect( 2 );
|
||||
var element = $( "#spin" ).spinner({
|
||||
value: .05,
|
||||
step: .0001
|
||||
});
|
||||
element.spinner( "stepUp" );
|
||||
equal( element.val(), "0.0501", "precision from step" );
|
||||
|
||||
element.spinner( "option", {
|
||||
value: 1.05,
|
||||
step: 1
|
||||
});
|
||||
element.spinner( "stepDown" );
|
||||
equal( element.val(), "0.05", "precision from value" );
|
||||
});
|
||||
|
||||
})( jQuery );
|
||||
|
13
ui/jquery.ui.spinner.js
vendored
13
ui/jquery.ui.spinner.js
vendored
@ -228,10 +228,13 @@ $.widget( "ui.spinner", {
|
||||
this.counter = 1;
|
||||
}
|
||||
|
||||
var newVal = this.value() + step * this._increment( this.counter );
|
||||
var newVal = this.value() + step * this._increment( this.counter ),
|
||||
// fix precision from bad JS floating point math
|
||||
precision = Math.max( this._precision( this.value() ),
|
||||
this._precision( this.options.step ) );
|
||||
|
||||
// clamp the new value
|
||||
newVal = this._trimValue( newVal );
|
||||
newVal = this._trimValue( newVal.toFixed( precision ) );
|
||||
|
||||
if ( !this.spinning || this._trigger( "spin", event, { value: newVal } ) !== false) {
|
||||
this._value( newVal );
|
||||
@ -245,6 +248,12 @@ $.widget( "ui.spinner", {
|
||||
1;
|
||||
},
|
||||
|
||||
_precision: function( num ) {
|
||||
var str = num.toString(),
|
||||
decimal = str.indexOf( "." );
|
||||
return decimal === -1 ? 0 : str.length - decimal - 1;
|
||||
},
|
||||
|
||||
_trimValue: function( value ) {
|
||||
var options = this.options;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user