2012-11-30 8 views
7

Ich habe den folgenden Code. Funktioniert wunderbar abgesehen von i der endgültig tinstotal Variable soll auf die nächsten 1 bis aufzurundenAbrunden der endgültigen Ausgabe in jquery

$(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) 

}); 
}); 

das Skript ist hier aktiv (2,04 wird auf 3 usw. aufgerundet) an http://andyholmes.me/sitewizard/index.html im roten Feld in der Nähe der Unterseite. Hoffe ihr könnt helfen, danke!

Antwort

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

Math.ceil() wird Runde auf die nächste ganze Zahl

+0

Sie verwenden bereits Math.Ceil() Es ist also nicht sinnvoll, .toFixed (2) zu verwenden –

1

Einsatz Javascript Math.round()

ex: 
var a = Math.round(2.60); 
var b = Math.round(-2.60); 
var c = Math.round(2.49); 

Result : 
alert(a); => 3 
alert(b); => -3 
alert(c); => 2 

Hoffnung hilft Ihnen.