我有以下代码。除了我希望将最终的tintotal变量四舍五入到最接近的1(2.04被四舍五入到3等)之外,效果还不错

$(document).ready(function(){

// Animate logo
$('#Logo').hide().fadeIn(800);

// Calculation Scripts
// Square Metres
var output = $('#SquareMetres'),
    tinoutput = $('#Tins'),
    priceoutput = $('#Price');
$('input[type="text"]').keyup(function() {
var width = parseFloat( $('#width').val()),
    height = parseFloat( $('#height').val()),
    result = height * width / 10000,
    finalresult = result.toFixed(2);
if (isNaN(result)) return;

output.text(finalresult);

// Tins
var tinmetres = 32.5,
    tinprice = 18.23,
    tinsresult = finalresult / tinmetres;
    tinstotal = tinsresult.toFixed(2);

tinoutput.text(tinstotal);

var price = tinstotal * tinprice,
    totalprice = price.toFixed(2);

priceoutput.text('£'+totalprice)

});
});


该脚本在底部附近红色框中的http://andyholmes.me/sitewizard/index.html处处于活动状态。希望你们能有所帮助,谢谢!

最佳答案

tinstotal = Math.ceil(tinsresult);
tinoutput.text(tinstotal);


Math.ceil()将四舍五入到下一个整数

10-02 14:26