View Full Version : Round to nearest hundred?

09-18-2008, 07:36 PM
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!

09-18-2008, 07:56 PM
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!! :o


09-18-2008, 07:58 PM

seems to do the trick. Yay Google!

09-18-2008, 08:00 PM
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!


09-18-2008, 08:54 PM
Let's just say this: I like making my own toolboxesAlso 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():
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

09-18-2008, 08:56 PM
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... :)


09-18-2008, 09:01 PM
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.

09-18-2008, 09:04 PM
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.