Results 1 to 8 of 8

Thread: Round to nearest hundred?

  1. #1
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default Round to nearest hundred?

    So I have user-generated number from 100 to 200000, and I would like to be able to round the number to the nearest 100. So for example, if the number I get from the user is 34568, it should round to 34600.

    Any advice greatly appreciated!

  2. #2
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    I know it!! Yes, I have got it!! Take that number that you have, say, 34568, and place it in a string, but before that, place it in a regular number variable. Next, make a function that checks to see how long the string is w/ your number in it using the .length extention. Place that number in a variable. Take that variable w/ the length in it and subtract it by three (3) and place that answer in a variable. Next, simply use the .charAt(x) extention on the variable with the string in it with the value of the variable containing the number recieved by subtracting the string's length by 3. Ex. some_var_with_string.charAt(variable_which_holds_difference_of_string_length_and_three). This would get the digit next to the 5, which is 6. You may then wish to implement a function that checks to see if this digit is greater than ( > ) 5. If so, then do this: some_var_with_string.charAt((variable_which_holds_difference_of_string_length_and_three-1)) = (that_number+1); otherwise, change the last two digits to 0. All you have to do next is change the rest of the string to 0 and you have the rounded number which you may output to the user. You may wish to set aside MANY variables to do this. To get the information from the string, you must escape(yourstringhere) first to get what you need. I know this is a tad complicated, but if you read the whole post, I'm sure you'll get it. You may also wish to read CAREFULLY and PRECISELY. Please, feel free to ask questions. Sorry if this is hard to understand!!

    -magicyte
    Last edited by magicyte; 09-18-2008 at 09:01 PM.

  3. #3
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    Math.round(someNumber/100)*100

    seems to do the trick. Yay Google!

  4. #4
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    My answer was more complicated and it didn't use currently built-in functions. Let's just say this: I like making my own toolboxes, but other users like the SHORTCUT for programming... But, true, that would bring out the answer of 346.00 * 100 = 34600. heh heh!

    -magicyte
    Last edited by magicyte; 09-18-2008 at 08:05 PM.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Let's just say this: I like making my own toolboxes
    Also known as 'reinventing the wheel'

    I seem to remember your sig a little while back: 'judge code by its speed, size, and portability'. You missed out 'elegance', which brings the total number of criteria on which the number-to-string-and-back-again method fails to three.

    If you really wanted to avoid Math.round() for some strange reason, you would do it with Number.prototype.toFixed(): +x.toFixed(-2). It still involves a transformation to string and back, though. Perhaps one could implement one's own round():
    Code:
    var NotMath = (function() {
      function round(n, places) {
        var magn = pow(10, -places || 0);
            units = n % magn;
        return units >= (magn / 2) ? n + (1 - units) : n - units;
      }
    
      function pow(n, m) {
        var r = 1;
    
        if (m > 0)
          while (m--)
            r *= n;
        else if (m < 0)
          while (m++)
            r /= n;
    
        return r;
      }
    
      return {
        round: round,
        pow: pow
      };
    })();
    Last edited by Twey; 09-18-2008 at 09:14 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Yeah. Everybody does that - so one would think. But at least I make my own toolboxes, even though some people may have the same code.

    By the way, I wonder who invented the wheel...

    -magicyte

  7. #7
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    What's the order of operations in my solution? Is the quotient of someNumber/100 being rounded first and then being multiplied by 100?

    I suck at math.

  8. #8
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Well, when you say Math.round(34568/100) * 100, first we do parinthesis. 34568 divided by 100 = 345.68. When rounded, it produces: 346.00. Multiplying that number by 100 gives you 34600.00, or 34600. So, to your question, yes.

    -magicyte

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
  •