SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Rounding ...

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Sep 2003
    Location
    Kingston
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry Rounding ...

    Guys,

    This problem has been bugging me for a couple days now ... I hope someone can help me.

    I have to round a fractional value from a form input to either 0, 25, 50 or 75. I have succesfully tested the value and stripped the decimal place. If the value is for example .1, I round it to 0. If the value is say .2, I round it to .25. My problem is that when the value is > .75, my function is returning undefined. For the sake of brevity I will include only the relevant code.

    Code:
    var increments = new Array(0,25,50,75); 
    
    CharBefore = parseFloat(CharBefore); //the whole number
    CharAfter = parseFloat(CharAfter);   //the decimal
    
    var x = parseFloat(increments.length - 1) ;
    
    for ( i = 0; i <=  x ; i++  ) {
    							
       var testValue = parseFloat(increments[i]);
       if (CharAfter > testValue) { continue ; }
       else {	
           var lowerValue = parseFloat(increments[i-1]);
    	if ((CharAfter > testValue) && (i==x)) { 
    
    	    median =  (testValue + 100)/2 ;
    	    if (CharAfter >= median) {
                       CharAfter = '0'; 
                       CharBefore++;} //round up to next whole number
    	else { CharAfter = testValue; }
    	    
                     }
    	else {
    	median =  (testValue + lowerValue)/2 ;
                   if (CharAfter >= median) { CharAfter = testValue; }
    	  else { CharAfter = lowerValue; }
    
                }
    	return  CharBefore + '.' + CharAfter;
    	break;
    							
         } 
    						
    } // End for loop
    Any help is greatly appreciated.
    Last edited by ole dawg; Nov 9, 2003 at 15:56.

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What should it return? should it always round down to .75?

    Anyhow, here's a vastly simplified algorithm for the rest of it
    Code:
    function rounder( number )
    {
    	return Math.round( number * 4 ) / 4;
    }
    
    alert( rounder( .1 ) );
    alert( rounder( .2 ) );
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Addict
    Join Date
    Sep 2003
    Location
    Kingston
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    What should it return? should it always round down to .75?

    Anyhow, here's a vastly simplified algorithm for the rest of it
    Code:
    function rounder( number )
    {
    	return Math.round( number * 4 ) / 4;
    }
    
    alert( rounder( .1 ) );
    alert( rounder( .2 ) );
    frickin' amazing ...

    No, it shouldn't always round down to .75 ... your function does exactly what I need.

    Thanks so much for that !!!

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And: here's the correct way.
    Code:
    Number.prototype.rounder = function ()
    {
    	return Math.round( this * 4 ) / 4;
    }
    
    x = .1
    alert( x.rounder() );
    x = .2
    alert( x.rounder() );
    ::: certified wild guess :::

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oooh! Caught off guard by my own critique!

    Funny that you post this whilst I'm writing my javascript scope article =)
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    SitePoint Addict
    Join Date
    Sep 2003
    Location
    Kingston
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    adios, thanks,

    The function was already written though, I just needed the rounding part of the decimal done.

    The relvant parts of the code is now

    Code:
    CharAfter = parseFloat(CharAfter);
    CharAfter = Math.round( CharAfter * 4 ) / 4;
    return  CharBefore + CharAfter;
    I needed to automatically round any decimal portions of a number into .25 increments, so ...

    1.1 = 1
    1.19 = 1.25
    1.41 = 1.5

    etc.

    I know the Math object and round function, but just never thought about using them like this. Brain has been fried recently ... to much coding!!

    Thanks again guys.

  7. #7
    SitePoint Addict
    Join Date
    Sep 2003
    Location
    Kingston
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ... and now I see that I don't need to be parsing any numbers either.... the one line function does it all ...

    Even better ... more thanks !!


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
  •