473,594 Members | 3,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

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
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.

Similar topics

3
332
by: b | last post by:
Hello all, we have a table that is part of our accounting package that stores decimals as such: 123.45678. However the reporting standards with the accounting system display that as 123.45 note that it does not round to the 123.46. I need for these reports to match up however in my datagrid which i have set for currency it does the rounding.. Does anyone know a way around this.. I Have looked around on the web but yet to find anything...
4
15373
by: vooose | last post by:
Consider a rounding up function: public static decimal RoundUp(decimal val, decimal round) { return ((decimal)Math.Ceiling((double)(val/round)))*round; } Math.Ceiling (and Math.Floor for RoundDown) only take double,s so we need to cast twice. Is there a better way?
4
7820
by: spebola | last post by:
I am using vb.net 2003 professional and I get the following results when using the round method: dim Amount as decimal = 180.255 Amount = Amount.Round(Amount, 2) Amount now contains 180.25. I need it to contain 180.26. Any ideas?
8
2079
by: Zorpiedoman | last post by:
Howcome: Dim D as decimal = .5D msgbox d.Round(D, 0) this returns "0" Now when I went to school .5 rounds UP to 1 not DOWN to zero?????!!! Documentation says this, but what the heck are they thinking??? I just don't
6
4587
by: Jeff Boes | last post by:
(asked last week on .questions, no response) Can anyone explain why this happens? (under 7.4.1) select '2004-05-27 09:00:00.500001-04' :: timestamp(0) ; timestamp --------------------- 2004-05-27 09:00:01
12
13633
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically put the following code into your C# app: float testFloat2 = (int) (4.2f * (float)100); Console.Out.WriteLine("1: "+testFloat2); and the result will be 419
2
9194
by: Mr. Ken | last post by:
Here are the funny results I got from Dev-C++, what I need is rounding to nearest integer. How can I do that in Dev-C++? a = -1 -1 -1 -1 1 1 2 2 2 1 b = round(a) -0.729627 -0.9540721 -0.2123411 -0.078923 0.321015 0.9876552 1.123422 1.632136 1.234538 0.765442 c = int(a)
29
3156
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
3
8503
by: Nel222 | last post by:
Hi there, Can anyone help me sort out a rounding problem with Access? I am trying to round a value in a field up or down to the nearest 500. * = I want the current value field to round up or down to the nearest 500. I am using the following script, which works for the most part, except that when the is 500, 1500, 2500, etc., it will round it up to the next 1000. At 2500, it is already rounded to the nearest 500, so I don't want it...
30
29518
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As expected my_round(2.5) = 2 # Not 3, which is an odd num I'm interested in rounding numbers of the form "x.5" depending upon whether x is odd or even. Any idea about how to implement it ?
0
8255
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8010
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6665
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5413
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3868
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.