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! 14 5793
"calan" <no**@nospam.com> 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/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
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 ...
--`
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)+"<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
excellent!
Thanks guys!
"Robi" <me@privacy.net> 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)+"<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
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)+"<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 ;-)
JRS: In article <Dc******************************@trueband.net>, 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(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 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
"Dr John Stockton" <jr*@merlyn.demon.co.uk> wrote in message news:lr**************@merlyn.demon.co.uk... JRS: In article <Dc******************************@trueband.net>, 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(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 :)
JRS: In article <M8********************@trueband.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(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.
<URL:http://www.merlyn.demon.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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
"calan" <no**@nospam.com> 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
Albert Grennock wrote: "calan" <no**@nospam.com> wrote in messageDoes anyone have a function that will round a number to 0 or .5?
<snip> 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
--.
Albert Grennock <me@privacy.net> wrote:
(OP has not reach my server, so I am piggybacking. Apologies.) "calan" <no**@nospam.com> 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.
Dr Clue <ia*********@mindspring.com> 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.
Christopher Benson-Manica wrote: Dr Clue <ia*********@mindspring.com> 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).
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
--.
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:dg**********@chessie.cirr.com... Albert Grennock <me@privacy.net> wrote:
(OP has not reach my server, so I am piggybacking. Apologies.)
"calan" <no**@nospam.com> 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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. ...
|
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...
|
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
---------------------...
|
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...
|
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...
|
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
|
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...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
| |