Round up final output in jquery

80,446

Solution 1

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

Math.ceil() will round to the next whole number

Solution 2

use 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

Hope is helps you.

Solution 3

I would imagine you are looking for Math.ceil()

Math.ceil(7.004);
// expected output: 8

Math.ceil(-7.004);
// expected output: -7

Solution 4

priceoutput.text('£'+ Math.ceil(totalprice));

Solution 5

use javascript's Math.ceil() method to round numbers up.

you can view information about this method here:

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

Share:
80,446
Andy Holmes
Author by

Andy Holmes

Passionate about design and development. Love being thrown in at the deep & learning hands on.

Updated on September 22, 2021

Comments

  • Andy Holmes
    Andy Holmes over 2 years

    I have the following code. Works beautifully apart from i want the final tinstotal variable to be rounded up to the nearest 1 (2.04 being rounded up to 3 etc)

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

    the script is active here at http://andyholmes.me/sitewizard/index.html in the red box near the bottom. Hope you guys can help, thanks!

  • Amar Singh
    Amar Singh almost 8 years
    You are already using Math.Ceil() So there is not point of using .toFixed(2)
  • User
    User over 3 years
    This does not round up as requested. Your example clearly shows it rounds to nearest int.