473,594 Members | 3,003 Online

# rounding a value to nearest 1/2

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 5818
"calan" <no**@nospam.co m> 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: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 9 '05 #2
calan wrote:
Does anyone have a function that will round a number to 0 or .5?

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
calan wrote in message news:og******** *********@newss vr12.news.prodi gy.com...
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(DV alue, DPrecision){
return Math.round(DVal ue / DPrecision) * DPrecision;
}

document.write( "22.24 -> "+DecimalRound( 22.24, 0.5)+"<br>");
document.write( "22.25 -> "+DecimalRound( 22.25, 0.5)+"<br>");
document.write( "22.52 -> "+DecimalRound( 22.52, 0.5)+"<br>");
document.write( "22.74 -> "+DecimalRound( 22.74, 0.5)+"<br>");
document.write( "22.751 -> "+DecimalRound( 22.751, 0.5)+"<br><br>" );

this way, if you need a different precision, you just change it:

document.write( "22.24 -> "+DecimalRound( 22.24, 0.25)+"<br>");
document.write( "22.25 -> "+DecimalRound( 22.25, 0.25)+"<br>");
document.write( "22.52 -> "+DecimalRound( 22.52, 0.25)+"<br>");
document.write( "22.74 -> "+DecimalRound( 22.74, 0.25)+"<br>");
document.write( "22.751 -> "+DecimalRound( 22.751, 0.25)+"<br>");

HTH

Sep 9 '05 #4
excellent!

Thanks guys!
"Robi" <me@privacy.net > wrote in message
news:Dc******** *************** *******@trueban d.net...
calan wrote in message

news:og******** *********@newss vr12.news.prodi gy.com...
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(DV alue, DPrecision){
return Math.round(DVal ue / DPrecision) * DPrecision;
}

document.write( "22.24 -> "+DecimalRound( 22.24, 0.5)+"<br>");
document.write( "22.25 -> "+DecimalRound( 22.25, 0.5)+"<br>");
document.write( "22.52 -> "+DecimalRound( 22.52, 0.5)+"<br>");
document.write( "22.74 -> "+DecimalRound( 22.74, 0.5)+"<br>");
document.write( "22.751 -> "+DecimalRound( 22.751, 0.5)+"<br><br>" );

this way, if you need a different precision, you just change it:

document.write( "22.24 -> "+DecimalRound( 22.24, 0.25)+"<br>");
document.write( "22.25 -> "+DecimalRound( 22.25, 0.25)+"<br>");
document.write( "22.52 -> "+DecimalRound( 22.52, 0.25)+"<br>");
document.write( "22.74 -> "+DecimalRound( 22.74, 0.25)+"<br>");
document.write( "22.751 -> "+DecimalRound( 22.751, 0.25)+"<br>");

HTH

Sep 9 '05 #5
calan wrote in message news:pJ******** *********@newss vr12.news.prodi gy.com...
excellent!

Thanks guys!
Robi wrote in message news:Dc******** *************** *******@trueban d.net...
calan wrote in message news:og******** *********@newss vr12.news.prodi gy.com...
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(DV alue, DPrecision){
return Math.round(DVal ue / DPrecision) * DPrecision;
}

document.write( "22.24 -> "+DecimalRound( 22.24, 0.5)+"<br>");
document.write( "22.25 -> "+DecimalRound( 22.25, 0.5)+"<br>");
document.write( "22.52 -> "+DecimalRound( 22.52, 0.5)+"<br>");
document.write( "22.74 -> "+DecimalRound( 22.74, 0.5)+"<br>");
document.write( "22.751 -> "+DecimalRound( 22.751, 0.5)+"<br><br>" );

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
JRS: In article <Dc************ *************** ***@trueband.ne t>, dated
Fri, 9 Sep 2005 17:01:44, seen in news:comp.lang. javascript, Robi
<me@privacy.net > posted :
for those kinds of rounding functions I like to use:

function DecimalRound(DV alue, DPrecision){
return Math.round(DVal ue / 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(Valu e * Precision) / Precision }

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 11 '05 #7

"Dr John Stockton" <jr*@merlyn.dem on.co.uk> wrote in message news:lr******** ******@merlyn.d emon.co.uk...
JRS: In article <Dc************ *************** ***@trueband.ne t>, dated
Fri, 9 Sep 2005 17:01:44, seen in news:comp.lang. javascript, Robi
<me@privacy.net > posted :
for those kinds of rounding functions I like to use:

function DecimalRound(DV alue, DPrecision){
return Math.round(DVal ue / 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(Valu e * 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(DV alue, DPrec){
DValue*=100; DPrec*=100;
return (Math.round(DVa lue / 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.50199999999 9995 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(Valu e * 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
JRS: In article <M8************ ********@trueba nd.net>, dated Mon, 12
Sep 2005 08:47:32, seen in news:comp.lang. javascript, Robi
<me@privacy.net > posted :

I just realised that you probably meant

function PR(Value, Precision){
Precision=1 / Precision;
return Math.round(Valu e * 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(Valu e * 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.

<URL:http://www.merlyn.demo n.co.uk/js-round.htm>.
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 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 12 '05 #9

"calan" <no**@nospam.co m> wrote in message
news:og******** *********@newss vr12.news.prodi gy.com...
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.9999999999999 999999 rather than 3.0

Sep 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.