如何在JavaScript中舍入到最接近的整数

时间:2016-09-22 01:51:16

标签: javascript rounding

您好我正在使用连接到我的html代码的JavaScript来制作转换计算器。我遇到的问题是如何将我的结果变成一个舍入的整数。我是JavaScript的新手,已尝试过多次尝试将Math.round(report1)或Math.round(report2)用于我的代码。也许我必须创建一个单独的功能?我很难过,并会感谢任何帮助。

//displaing reported values...for kg to pounds
var report1 = function (kilogram, pound) {
    document.getElementById("result").innerHTML =
        kilogram + "kg = " + pound + "lb";
};


//displaing reported values...for pounds to kilograms
var report2 = function (pound, kilogram) {
    document.getElementById("result").innerHTML =
        pound + "lb = " + kilogram + "kg";
};

//reading in value from lb to kg button
document.getElementById("p_to_kg").onclick = function () {
    var p = document.getElementById("weight").value;
    report2(p, p_to_kg(p));
};

//formula for pounds to kilograms
function p_to_kg(pound_weight) {
    return (pound_weight / 2.2);
}

//reading in value from kg to lb button
document.getElementById("kg_to_p").onclick = function () {
    var kg = document.getElementById("weight").value;
    report1(kg, kg_to_p(kg));
};

//formula for kilograms to pounds
function kg_to_p(kilogram_weight) {
    return (2.2 * kilogram_weight);
}

这是我的问题:

how to round this

2 个答案:

答案 0 :(得分:2)

HTML输入值类型是字符串,因此要计算它们的舍入值,您应该将它们转换为数字。有很多方法可以将它们转换为数字,例如使用Number值调用string或添加plus sign(+)(或除法或乘法):

var val = document.querySelector('#val');
var k2l = document.querySelector('#kg-to-lb');
var l2k = document.querySelector('#lb-to-kg');
var res = document.querySelector('#result');

//displaing reported values...for kg to pounds
var report1 = function(kilogram, pound) {
  result.innerHTML =
    kilogram + "kg = " + pound + "lb";
};


//displaing reported values...for pounds to kilograms
var report2 = function(pound, kilogram) {
  result.innerHTML = pound + "lb = " + kilogram + "kg";
};

k2l.addEventListener('click', function(e) {
  var kilogram = val.value;
  var pound = Math.round(kilogram * 2.2);
  report1(kilogram, pound);
});
l2k.addEventListener('click', function(e) {
  var pound = val.value;
  var kilogram = Math.round(pound / 2.2);
  report2(pound, kilogram);
});
value
<input id="val" type="number" />

<button id="kg-to-lb">kg 2 lb</button>
<button id="lb-to-kg">lb 2 kg</button>
<div id="result"></div>

答案 1 :(得分:1)

您可以使用数学对象的三种方法和数字中的一种方法:

var f = 56.14523;

console.log('f: '+f);
console.log('Math.round: '+Math.round(f));
console.log('Math.ceil: '+Math.ceil(f));
console.log('Math.floor: '+Math.floor(f));
console.log('Number.toFixed: '+f.toFixed(2));

输出结果为:

  

f:56.14523

     

Math.round:56

     

Math.ceil:57

     

Math.floor:56

     

Number.toFixed:56.15

如果小数点 .5 或更高,

Math.round 将向上舍入到下一个整数。

Math.ceil 将始终将值增加到下一个整数。

Math.floor 将始终将值减小到下一个整数。

Number.toFixed 将四舍五入为参数传递的小数位数。

如果要从输入中获取值,则必须先使用 parseFloat 将它们从字符串转换为浮点值。例如:

function processNumber() {
    var f = document.getElementById("float-input").value;
    f = parseFloat(f);
    if (isNaN(f)) {
        console.log(f+" is not a number");
    } else {
        console.log(f+" rounded is "+Math.round(f));
    }
    return "Temperature: " + Math.round(f) + " degrees";
}

document.getElementById("output-div").textContent = processNumber();