SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Question on rounding off a sum

    Hi. I have this JavaScript function that displays the sum of all the values of a column. Is there any way that I can modify the code to round the sum off to two decimal points?


    Code:
    		function calculateFooterValues(stage){
    			if(stage && stage!=2)
    				return true;
    			var srQ = document.getElementById("sr_q");
    				srQ.innerHTML = "$"+sumColumn(6)
    			return true;			
    		}
    		function sumColumn(ind){
    			var out = 0;
    			for(var i=0;i<mygrid.getRowsNum();i++){
    				out+= parseFloat(mygrid.cells2(i,ind).getValue())
    			}
    			return out;


  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You need Math.round. You'll need to apply that to out before returning it.

  3. #3
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    You need Math.round. You'll need to apply that to out before returning it.
    Thanks for replying. Is there a way to have math.round round off two places after the decimal point? For example, the result I get for out is

    $34376.49999999998

    and I'd like the result to be

    $34376.50

  4. #4
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks again! I think I'm getting close. I've modified the code to this:

    Code:
    function sumColumn(ind){
    			var out = 0;
    			for(var i=0;i<mygrid.getRowsNum();i++){
    				out+= parseFloat(mygrid.cells2(i,ind).getValue())
    			}
    			return Math.round(out * 100.00) / 100 ;out;
    		}
    Now I'm getting this value for out:

    $34376.5

    Please tell me: How can I get this value, instead?

    $34376.50

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Use toFixed
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by ScallioXTX View Post
    Use toFixed
    U TH' MAN! This worked:

    Code:
    function sumColumn(ind){
    			var out = 0;
    			for(var i=0;i<mygrid.getRowsNum();i++){
    				out+= parseFloat(mygrid.cells2(i,ind).getValue())
    			}
    			return out.toFixed(2) ;out;
    		}

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Note that .toFixed doesn't round. So 1.059 will be returned as 1.05 (should be 1.06).

    If you also want rounding you should probably do Math.round(number).toFixed(2)



    No, it does round. But please check a view inputs to see if you get what you want. The back of my mind is telling me there's something odd about .toFixed() ...
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  8. #8
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Quote Originally Posted by ScallioXTX View Post
    Note that .toFixed doesn't round. So 1.059 will be returned as 1.05 (should be 1.06).

    If you also want rounding you should probably do Math.round(number).toFixed(2)



    No, it does round. But please check a view inputs to see if you get what you want. The back of my mind is telling me there's something odd about .toFixed() ...
    OK. Someone at another forum suggested this:

    Code:
    		function sumColumn(ind){
    			var out = 0;
    			for(var i=0;i<mygrid.getRowsNum();i++){
    				out+= parseFloat(mygrid.cells2(i,ind).getValue())
    			}
    			out = "" + (Math.round(out * 100)) / 100 ; 
    			if (out.indexOf(".") == out.length-2) { out +='0' ; 
    			} 
    			return out ; 			
    		}
    Seems to work. What do you think?


  9. #9
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Yup looks fine
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  10. #10
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It may work but have you validated the code?

    http://www.jslint.com/

    The following validates:
    Code JavaScript:
    function sumColumn(ind) {
    	var out = 0, i, mygrid;
     
    	for (i = 0; i < mygrid.getRowsNum(); i += 1) {
    		out += parseFloat(mygrid.cells2(i, ind).getValue());
    	}
     
    	out = "" + (Math.round(out * 100)) / 100; 
     
    	if (out.indexOf(".") === out.length - 2) { 
    		out += "0";  
    	} 
     
    	return out;
    }

  11. #11
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If in doubt combine round and tofixed (that way you take care of any rounding issues with tofixed).

    Code:
    function sumColumn(ind) {
        var out = 0, i, mygrid;
        
        for (i = 0; i < mygrid.getRowsNum(); i += 1) {
            out += parseFloat(mygrid.cells2(i, ind).getValue());
        }
    return ((Math.round(out * 100)) / 100).toFixed(2);
    }
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •