@LinusKleen has what you want:
var num = + $(this).val();
if (num > 9) {
alert('message');
} else {
$(this).val(Math.ceil(num * 10) / 10);
}
Question
I currently have this which rounds up any input number to the nearest whole value on blur:
$(function(){
$('#my_value').blur(function() {
$(this).val(Math.ceil($(this).val()));
});
});
The problem is I need it to round to the next nearest 1/10th, so if someone input 1.23 it would round up to 1.3. Or if someone input 3.78 it would round up to 3.8.
Additionally I'd like it to display an alert when a value above 9 is entered into the #my_value field.
Thanks in advance.
La solution
@LinusKleen has what you want:
var num = + $(this).val();
if (num > 9) {
alert('message');
} else {
$(this).val(Math.ceil(num * 10) / 10);
}
Autres conseils
Try something like this
function nearest(value, min, max, steps){
var zerone = Math.round((value-min)*steps/(max-min))/steps; // bring to 0-1 range
return zerone*(max-min) + min;
}
console.log(nearest(2.75, 0, 10, 10)); // 3
console.log(nearest(5.12, 5, 6, 10)); // 5.1
console.log(nearest(0.06, 0, 1, 10)); // 0.1
$('#my_value').blur(function() {
if($(this).val() >9 )
{
alert("message");
}
else
nearest($(this).val(),0,9,10);
});
or try below
if val= 28.45 means
Math.round($(this).val()*10)/10 //returns 28.5