459,287 Members | 1,561 Online
Need help? Post your question and get tips & solutions from a community of 459,287 IT Pros & Developers. It's quick & easy.

rounding a value to nearest 1/2

 P: n/a Does anyone have a function that will round a number to 0 or .5? I have a form where I'm entering a number in inches. I need to round it to the nearest 1/2 inch (onChange). The split will be on increments of .25 22.24 = 22.0 22.25 = 22.5 22.52 = 22.5 22.74 = 22.5 22.751 = 23.0 TIA! Sep 9 '05 #1
14 Replies

 P: n/a "calan" writes: Does anyone have a function that will round a number to 0 or .5? function roundHalf(n) { return Math.round(n*2)/2; } /L -- Lasse Reichstein Nielsen - lr*@hotpop.com DHTML Death Colors: 'Faith without judgement merely degrades the spirit divine.' Sep 9 '05 #2

 P: n/a calan wrote: Does anyone have a function that will round a number to 0 or .5? How about this Math.round((22.25*2))/2) I have a form where I'm entering a number in inches. I need to round it to the nearest 1/2 inch (onChange). The split will be on increments of .25 22.24 = 22.0 22.25 = 22.5 22.52 = 22.5 22.74 = 22.5 22.751 = 23.0 TIA! -- --. --=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML/CSS,Javascript,TCP ... --` Sep 9 '05 #3

 P: n/a calan wrote in message news:og*****************@newssvr12.news.prodigy.co m... Does anyone have a function that will round a number to 0 or .5? I have a form where I'm entering a number in inches. I need to round it to the nearest 1/2 inch (onChange). The split will be on increments of .25 22.24 = 22.0 22.25 = 22.5 22.52 = 22.5 22.74 = 22.5 22.751 = 23.0 for those kinds of rounding functions I like to use: function DecimalRound(DValue, DPrecision){ return Math.round(DValue / DPrecision) * DPrecision; } document.write("22.24 -> "+DecimalRound(22.24, 0.5)+"
"); document.write("22.25 -> "+DecimalRound(22.25, 0.5)+"
"); document.write("22.52 -> "+DecimalRound(22.52, 0.5)+"
"); document.write("22.74 -> "+DecimalRound(22.74, 0.5)+"
"); document.write("22.751 -> "+DecimalRound(22.751, 0.5)+"

"); this way, if you need a different precision, you just change it: document.write("22.24 -> "+DecimalRound(22.24, 0.25)+"
"); document.write("22.25 -> "+DecimalRound(22.25, 0.25)+"
"); document.write("22.52 -> "+DecimalRound(22.52, 0.25)+"
"); document.write("22.74 -> "+DecimalRound(22.74, 0.25)+"
"); document.write("22.751 -> "+DecimalRound(22.751, 0.25)+"
"); HTH Sep 9 '05 #4

 P: n/a excellent! Thanks guys! "Robi" wrote in message news:Dc******************************@trueband.net ... calan wrote in message news:og*****************@newssvr12.news.prodigy.co m... Does anyone have a function that will round a number to 0 or .5? I have a form where I'm entering a number in inches. I need to round it to the nearest 1/2 inch (onChange). The split will be on increments of .25 22.24 = 22.0 22.25 = 22.5 22.52 = 22.5 22.74 = 22.5 22.751 = 23.0 for those kinds of rounding functions I like to use: function DecimalRound(DValue, DPrecision){ return Math.round(DValue / DPrecision) * DPrecision; } document.write("22.24 -> "+DecimalRound(22.24, 0.5)+"
"); document.write("22.25 -> "+DecimalRound(22.25, 0.5)+"
"); document.write("22.52 -> "+DecimalRound(22.52, 0.5)+"
"); document.write("22.74 -> "+DecimalRound(22.74, 0.5)+"
"); document.write("22.751 -> "+DecimalRound(22.751, 0.5)+"

"); this way, if you need a different precision, you just change it: document.write("22.24 -> "+DecimalRound(22.24, 0.25)+"
"); document.write("22.25 -> "+DecimalRound(22.25, 0.25)+"
"); document.write("22.52 -> "+DecimalRound(22.52, 0.25)+"
"); document.write("22.74 -> "+DecimalRound(22.74, 0.25)+"
"); document.write("22.751 -> "+DecimalRound(22.751, 0.25)+"
"); HTH Sep 9 '05 #5

 P: n/a calan wrote in message news:pJ*****************@newssvr12.news.prodigy.co m... excellent! Thanks guys! Robi wrote in message news:Dc******************************@trueband.net ... calan wrote in message news:og*****************@newssvr12.news.prodigy.co m... Does anyone have a function that will round a number to 0 or .5? I have a form where I'm entering a number in inches. I need to round it to the nearest 1/2 inch (onChange). [...] for those kinds of rounding functions I like to use: function DecimalRound(DValue, DPrecision){ return Math.round(DValue / DPrecision) * DPrecision; } document.write("22.24 -> "+DecimalRound(22.24, 0.5)+"
"); document.write("22.25 -> "+DecimalRound(22.25, 0.5)+"
"); document.write("22.52 -> "+DecimalRound(22.52, 0.5)+"
"); document.write("22.74 -> "+DecimalRound(22.74, 0.5)+"
"); document.write("22.751 -> "+DecimalRound(22.751, 0.5)+"

"); BTW, this formula is used in the Swiss Monetary rounding system. In Switzerland the common rounding is 0.05 CHF (Swiss Francs) so the DPrecision value is 0.05 (that is 5 Rappen [5 cent]) 5 Rappen coins are the smallest commonly used coins, although there are officially 1 and 2 Rappen coins available but not used in the common payment system. Just for information purpose ;-) Sep 10 '05 #6

 P: n/a JRS: In article , dated Fri, 9 Sep 2005 17:01:44, seen in news:comp.lang.javascript, Robi posted :for those kinds of rounding functions I like to use:function DecimalRound(DValue, DPrecision){return Math.round(DValue / DPrecision) * DPrecision;} which in fact has nothing at all to do with decimal. Have you tried DecimalRound(3.315, 0.01) ?? Neither 3.315 nor 0.01 can be represented exactly. Consider instead function PR(Value, Precision){ return Math.round(Value * Precision) / Precision } -- © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 © JL/RC: FAQ of news:comp.lang.javascript jscr maths, dates, sources. TP/BP/Delphi/jscr/&c, FAQ items, links. Sep 11 '05 #7

 P: n/a "Dr John Stockton" wrote in message news:lr**************@merlyn.demon.co.uk... JRS: In article , dated Fri, 9 Sep 2005 17:01:44, seen in news:comp.lang.javascript, Robi posted :for those kinds of rounding functions I like to use:function DecimalRound(DValue, DPrecision){return Math.round(DValue / DPrecision) * DPrecision;} which in fact has nothing at all to do with decimal. Have you tried DecimalRound(3.315, 0.01) ?? I did now... floating point errors on this level are a bad omen.... Neither 3.315 nor 0.01 can be represented exactly. Consider instead function PR(Value, Precision){ return Math.round(Value * Precision) / Precision } Doktor, somehow your function doesn't get the floating point error but returns horribly rounded results... Va Pr Va*Pr rd /Pr 22.24 0.5 ->11.12 11 ->22 22.25 0.5 ->11.125 11 ->22 22.52 0.5 ->11.26 11 ->22 22.74 0.5 ->11.37 11 ->22 22.751 0.5 ->11.3755 11 ->22 3.315 0.1 -> 0.3315 0 -> 0 22.25 0.1 -> 2.225 2 ->20 22.52 0.01-> 0.2252 0 -> 0 3.315 0.01-> 0.03315 0 -> 0 I have now tried a different approach although haven't went too far down into the decimals, but so far it does seem to do the trick: function DecimalRound(DValue, DPrec){ DValue*=100; DPrec*=100; return (Math.round(DValue / DPrec) * DPrec)/100; } (original) | (DValue*100 and DPrec*100) (result/100) DValue DPrec DValue/DPrec round *DPrec 22.24 0.5 ->44.48 44 2200 ->22 22.25 0.5 ->44.5 45 2250 ->22.5 22.52 0.5 ->45.04 45 2250 ->22.5 22.74 0.5 ->45.48 45 2250 ->22.5 22.751 0.5 ->45.501999999999995 46 2300 ->23 3.315 0.1 ->33.15 33 330 -> 3.3 22.25 0.1 ->222.5 223 2230 ->22.3 22.52 0.01 ->2252 2252 2252 ->22.52 3.315 0.01 ->331.5 332 332 -> 3.32 22.751 0.01 ->2275.1 2275 2275 ->22.75 22.751 0.005 ->4550.2 4550 2275 ->22.75 22.752 0.005 ->4550.4 4550 2275 ->22.75 22.753 0.005 ->4550.6 4551 2275.5->22.755 I just realised that you probably meant function PR(Value, Precision){ Precision=1 / Precision; return Math.round(Value * Precision) / Precision } of course, it depends what the definition of "Precision" is. thanks for pointing out the floating point problem. my regular programming language has a better FP handling, that's why I didn't catch this :) Sep 12 '05 #8

 P: n/a JRS: In article , dated Mon, 12 Sep 2005 08:47:32, seen in news:comp.lang.javascript, Robi posted :I just realised that you probably meantfunction PR(Value, Precision){ Precision=1 / Precision; return Math.round(Value * Precision) / Precision }of course, it depends what the definition of "Precision" is. I don't really mean that, since (for 3.315 to 3.32) that relies on the reciprocal of an imprecise 0.01 being an exact 100 (which it is). I meant :- function PR(Value, Precision) { return Math.round(Value * Precision) / Precision } with Precision now being defined as the number of parts into which unity is divided, rather than as the size of each part. Part of the art of avoiding floating-point rounding errors is to use, as far as possible, values which will be stored exactly, even where it does not seem to matter. For rounding, Precision will generally be either a literal or a copy of a literal, rather than a calculated value. One should, therefore, use your function to round to a multiple of unity. . For General Info : I have a compact method for the date of Easter Sunday 1900-2199, derived directly from the front of the Church of England Book of Common Prayer, ca. 1960 (the 1980 ASB omits it). -- © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 © JL/RC: FAQ of news:comp.lang.javascript jscr maths, dates, sources. TP/BP/Delphi/jscr/&c, FAQ items, links. Sep 12 '05 #9

 P: n/a "calan" wrote in message news:og*****************@newssvr12.news.prodigy.co m... Does anyone have a function that will round a number to 0 or .5? I have a form where I'm entering a number in inches. I need to round it to the nearest 1/2 inch (onChange). The split will be on increments of .25 22.24 = 22.0 22.25 = 22.5 22.52 = 22.5 22.74 = 22.5 22.751 = 23.0 TIA! I don't really 'do' java but here is how I would do it in general terms. Using C type terms. Convert the number into a string eg "23.24", then take the last to digits of the string eg "24". Then all you have to do is say if the string is less then "25" the result is "00" else if the string is less then "75" the result is "50" else the result is "00" (but remember to add one to the over all result). Then convert back to a floating point number, adding 1 is necessary. It will be much quicker than doing any floating point arithmetic which is also likely to be inaccurate due to rounding problems. Anyway its pretty simple and avoids things like getting 2.9999999999999999999 rather than 3.0 Sep 21 '05 #10

 P: n/a Albert Grennock wrote: "calan" wrote in messageDoes anyone have a function that will round a number to 0 or .5? I don't really 'do' java but here is how I would do it in general terms. Using C type terms. Actually , we are speaking of "javascript" not "java". The distinction is rather important. Convert the number into a string eg "23.24", then take the last to digits of the string eg "24". Then all you have to do is say if the string is less then "25" the result is "00" else if the string is less then "75" the result is "50" else the result is "00" (but remember to add one to the over all result). Then convert back to a floating point number, adding 1 is necessary. It will be much quicker than doing any floating point arithmetic which is also likely to be inaccurate due to rounding problems. Anyway its pretty simple and avoids things like getting 2.9999999999999999999 rather than 3.0 Considering the OP's ( orignal poster's ) application , what improvement does all that kinetic energy give us over the common solution several of us gave as variations of the line below? Math.round((22.25*2))/2) -- --. --=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript --=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP --=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL --. Sep 21 '05 #11

 P: n/a Albert Grennock wrote: (OP has not reach my server, so I am piggybacking. Apologies.) "calan" wrote in message news:og*****************@newssvr12.news.prodigy.co m... Does anyone have a function that will round a number to 0 or .5? function half_round( num ) { var fraction_part=Math.floor( num*100 )%100; var int_part=Math.floor( num ); if( fraction_part < 25 ) { return int_part; } else if( fraction_part < 75 ) { return int_part+0.5; } return int_part+1; } (assuming, of course, you've played nicely and passed a number) It will be much quicker than doing any floating point arithmetic which is also likely to be inaccurate due to rounding problems. Anyway its pretty simple and avoids things like getting 2.9999999999999999999 rather than 3.0 I disagree. -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome. Sep 21 '05 #12

 P: n/a Dr Clue wrote: Considering the OP's ( orignal poster's ) application , what improvement does all that kinetic energy give us over the common solution several of us gave as variations of the line below? I haven't seen "several", but I do wish I had seen your post before I posted my version (although it's still better IMO than using the string idea). Oy. Math.round((22.25*2))/2) The only point I would raise is that this deserves a line of documentation (although admittedly I have written much obtuse JavaScript and omitted needed documentation). -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome. Sep 21 '05 #13

 P: n/a Christopher Benson-Manica wrote: Dr Clue wrote:Considering the OP's ( orignal poster's ) application , whatimprovement does all that kinetic energy give us overthe common solution several of us gave as variations of the line below? I haven't seen "several", but I do wish I had seen your post before I posted my version (although it's still better IMO than using the string idea). Oy.Math.round((22.25*2))/2) The only point I would raise is that this deserves a line of documentation (although admittedly I have written much obtuse JavaScript and omitted needed documentation). Your correct, but the sub-title of that message was "read the thread" :) Thus the intentionally obtuse reference. I simply copied that line from my other message , whose context makes the thing clear.There was another respondent that gave a full function body as well using the same logic. I find a response like the one I gave to be far better than yelling at them, as it might make them curious enough to RIFF. -- --. --=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript --=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP --=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL --. Sep 21 '05 #14

 P: n/a "Christopher Benson-Manica" wrote in message news:dg**********@chessie.cirr.com... Albert Grennock wrote: (OP has not reach my server, so I am piggybacking. Apologies.) "calan" wrote in message news:og*****************@newssvr12.news.prodigy.co m... Does anyone have a function that will round a number to 0 or .5? function half_round( num ) { var fraction_part=Math.floor( num*100 )%100; var int_part=Math.floor( num ); if( fraction_part < 25 ) { return int_part; } else if( fraction_part < 75 ) { return int_part+0.5; } return int_part+1; } (assuming, of course, you've played nicely and passed a number) It will be much quicker than doing any floating point arithmetic which is also likely to be inaccurate due to rounding problems. Anyway its pretty simple and avoids things like getting 2.9999999999999999999 rather than 3.0 I disagree. Maybe but my method requires no knowledge of libary functions or whatever. and probably runs faster. -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome. Sep 21 '05 #15