4 回答
TA贡献1796条经验 获得超7个赞
如果添加0.2到该值,则可以使用Math.round():
input.oninput = function() {
let lvalue = +document.getElementById('input').value + 0.2;
document.getElementById('output').innerHTML = Math.round(lvalue);
}
<input id="input" type="text">
<div id="output"></div>
TA贡献1813条经验 获得超2个赞
您可以使数字小于 1,那么它应该适用于所有情况:
let lvalue = $(".leave-input").val();
const baseValue = Math.round((lvalue - Math.floor(lvalue)) * 100) / 100;
if (baseValue<= 0.29) {
lvalue = roundDown();
} else if (baseValue>= 0.3) {
lvalue = roundUp();
}
这是一个工作示例:
function calculate() {
let lvalue = Number(document.getElementById('input').value)
const baseValue = Math.round((lvalue - Math.floor(lvalue)) * 100) / 100;
if (baseValue <= 0.29) {
lvalue = Math.floor(lvalue);
} else if (baseValue >= 0.3) {
lvalue = Math.ceil(lvalue);
}
document.getElementById('output').innerHTML = lvalue;
}
<input id="input" type="text" />
<button onClick="calculate()">Round</button>
<div id="output"></div>
TA贡献1828条经验 获得超3个赞
$(".leave-input").change(function(){
let lvalue = parseFloat($(".leave-input").val());
const val = lvalue * 100 % 100 ;
if (val <= 29) {
lvalue = Math.floor(lvalue);//roundDown();
} else {//your condition will skip anything between .29 and .30
lvalue = Math.ceil(lvalue);//roundUp();
}
$(".result").html(lvalue);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="leave-input" />
<div class="result"></div>
TA贡献1875条经验 获得超5个赞
let lvalue = $(".leave-input").val();
const integer = Math.trunc(lvalue);
const decimal = lvalue % 1;// or you could do lvalue - interger
if (integer >= 7 && integer <= 16) {
if (decimal <= 0.29) {
lvalue = roundDown();
} else if (decimal >= 0.3) {
lvalue = roundUp();
}
}
添加回答
举报