jquery

2012-11-30 27 views
7

में अंतिम आउटपुट को गोल करें मेरे पास निम्न कोड है। खूबसूरती से अलग से मैं चाहता हूँ अंतिम tinstotal चर निकटतम 1 में पूर्णांकित कर दिया करने के लिए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) 

}); 
}); 

लिपि (2.04 3 आदि में पूर्णांकित कर जा रहा है) काम करता है तल के पास लाल बॉक्स में http://andyholmes.me/sitewizard/index.html पर यहां सक्रिय है। आशा है कि आप लोग मदद कर सकते हैं, धन्यवाद!

उत्तर

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

Math.ceil() देख रहे होगा दौर अगले पूर्ण संख्या

+0

आप पहले से ही Math.Ceil() का उपयोग कर रहे हैं तो .toFixed (2) का उपयोग करने का कोई मतलब नहीं है। –

0

संख्या पूर्णांक बनाना जावास्क्रिप्ट का Math.ceil() विधि का उपयोग करें।

आप इस विधि का यहाँ के बारे में जानकारी देख सकते हैं:

http://www.w3schools.com/jsref/jsref_ceil.asp

1

उपयोग करने के लिए जावास्क्रिप्ट 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 

आशा आपकी मदद करता है।