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
14 5837
Albert Grennock wrote: "calan" <no**@nospam.co m> 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.9999999999999 999999 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.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?
function half_round( num ) {
var fraction_part=M ath.floor( num*100 )%100;
var int_part=Math.f loor( 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.9999999999999 999999 rather than 3.0
I disagree.
--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Dr Clue <ia*********@mi ndspring.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)cybers pace.org | don't, I need to know. Flames welcome.
Christopher Benson-Manica wrote: Dr Clue <ia*********@mi ndspring.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((2 2.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
--.
"Christophe r Benson-Manica" <at***@nospam.c yberspace.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.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? function half_round( num ) { var fraction_part=M ath.floor( num*100 )%100; var int_part=Math.f loor( 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.9999999999999 999999 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)cybers pace.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 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...
|
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?
|
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?
|
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
|
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
| |
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
|
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)
|
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 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...
|
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 ?
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |