Is there a script that will round off a number to a certain number of
decimal places? -Allen Thompson 22 2185
In article <Lx*************@newsread2.news.atl.earthlink.net> , Allen
Thompson <ge********@mindspring.com> wrote: Is there a script that will round off a number to a certain number of decimal places? -Allen Thompson
1. Let x = the number of decimal places you want.
2. Multiply the number by 10 raised to x.
3. Use Math.round() on the answer.
4. Divide by the number obtained in 2.
--
Dennis M. Marks
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Dennis M. Marks wrote: In article <Lx*************@newsread2.news.atl.earthlink.net> , Allen Thompson <ge********@mindspring.com> wrote:
Is there a script that will round off a number to a certain number of decimal places? -Allen Thompson
1. Let x = the number of decimal places you want. 2. Multiply the number by 10 raised to x. 3. Use Math.round() on the answer. 4. Divide by the number obtained in 2.
or use the toFixed() method of the Number object: http://devedge.netscape.com/library/...r.html#1193137
In article <br**********@sparta.btinternet.com>, bagbourne <no***@noway.com>
writes: Dennis M. Marks wrote:
<--snip--> or use the toFixed() method of the Number object:
alert((0.007).toFixed(2)
//gives 0.00
Which is dropping it to two decimal places, *not* rounding it.
--
Randy
In article <20***************************@mb-m11.aol.com>,
HikksNotAtHome <hi************@aol.com> wrote: In article <br**********@sparta.btinternet.com>, bagbourne <no***@noway.com> writes:
Dennis M. Marks wrote:
<--snip-->
or use the toFixed() method of the Number object:
alert((0.007).toFixed(2) //gives 0.00
Which is dropping it to two decimal places, *not* rounding it.
What kind of misquoting are you doing? My response was about
Math.round() NOT TOFIXED. MY RESPONSE WAS AS FOLLOWS:
1. Let x = the number of decimal places you want.
2. Multiply the number by 10 raised to x.
3. Use Math.round() on the answer.
4. Divide by the number obtained in 2.
--
Dennis M. Marks
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
"Dennis M. Marks" a ecrit : My response was about Math.round() NOT TOFIXED. MY RESPONSE WAS AS FOLLOWS: 1. Let x = the number of decimal places you want. 2. Multiply the number by 10 raised to x. 3. Use Math.round() on the answer. 4. Divide by the number obtained in 2.
and ???
x = Math.round(2*1000)/1000;
gives
x = 2
where are the missing .000 to obtain 2.000
--
************************************************** ************
Stéphane MORIAUX : mailto:st*********************@wanadoo.fr
Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS) http://perso.wanadoo.fr/stephanePOINTmoriaux/internet/
************************************************** ************
"Dennis M. Marks" <de******@dcsi.net> wrote in message
news:131220031506178104%de******@dcsi.net... In article <20***************************@mb-m11.aol.com>, HikksNotAtHome <hi************@aol.com> wrote:
In article <br**********@sparta.btinternet.com>, bagbourne <no***@noway.com> writes:
Dennis M. Marks wrote:
<--snip--> or use the toFixed() method of the Number object:
alert((0.007).toFixed(2) //gives 0.00
Which is dropping it to two decimal places, *not* rounding it.
What kind of misquoting are you doing? ...
<snip>
There is no misquoting, Randy is responding to bagbourne's reply to your
post and has quoted the parts of that post that he is responding to
verbatim.
Richard.
In article <3F***************@wanadoo.fr>, <"@SM"> wrote: "Dennis M. Marks" a ecrit :
My response was about Math.round() NOT TOFIXED. MY RESPONSE WAS AS FOLLOWS: 1. Let x = the number of decimal places you want. 2. Multiply the number by 10 raised to x. 3. Use Math.round() on the answer. 4. Divide by the number obtained in 2.
and ???
x = Math.round(2*1000)/1000; gives x = 2 where are the missing .000 to obtain 2.000
<snip>
The original question was:
Is there a script that will round off a number to a certain number of
decimal places? -Allen Thompson
It did not state "display" a certain number of decimal places. My
answer is mathematically correct. To show trailing zeros after the
decimal point text zeros would have to be affixed to the end. It's too
bad that javascript doesn't have output formatting like some other
languages.
--
Dennis M. Marks
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
JRS: In article <13*************************@dcsi.net>, seen in
news:comp.lang.javascript, Dennis M. Marks <de******@dcsi.net> posted at
Sat, 13 Dec 2003 08:25:39 :- In article <Lx*************@newsread2.news.atl.earthlink.net> , Allen Thompson <ge********@mindspring.com> wrote:
Is there a script that will round off a number to a certain number of decimal places?
That is not, as written, a meaningful question. All javascript numbers
are 64-bit IEEE Doubles, floating-point binary. Only strings can have
decimal places. Presumably you want to start with a variable (or
expression) of type Number, and generate a String representing that
number in decimal with two digits after the decimal point, probably for
display.
You should have read the FAQ before posting; all of it, but item 4.6
refers.
1. Let x = the number of decimal places you want. 2. Multiply the number by 10 raised to x. 3. Use Math.round() on the answer. 4. Divide by the number obtained in 2.
That is the answer to a different question than the one almost certainly
intended.
You too should have read the FAQ before posting. That gives the nearest
(give or take) Number which is a multiple of 10^-x. When displayed,
that result will be shown with no more than x digits after the point;
but there may be fewer. One might get # or #.# or #.## if x=2.
The version in the FAQ has
if (S.search && S.search(/\D/)!=-1) { return ''+X }
If S.search does not exist, but S.search(/\D/) would have succeeded, the
return does not occur. But the following code was written on the
assumption that the search had, or would have, failed.
I now use
if (/\D/.test(S)) { return ''+X }
on the basis that it should work in Javascript 1.2 and higher, which is
adequate.
Otherwise, is there a brief method of testing for a non-digit that works
in earlier javascript? Better than
for (x=0;x<S.length;x++) if ((t=S.charAt(x))<'0'||t>'9') return ''+X
?
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.
@SM wrote on 14 Dec 2003 at Sun, 14 Dec 2003 00:07:11 GMT: x = Math.round(2*1000)/1000; gives x = 2 where are the missing .000 to obtain 2.000
To be fair, the OP mentioned nothing about trailing zeros, just
performing rounding. They are not the same thing. The rounding may
just be part of a calculation.
If the question was: "How do I display a floating-point value to a
certain number of decimal places?", then you could argue about the
missing zeros.
Mike
--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk")
Dennis M. Marks hu kiteb: Which is dropping it to two decimal places, *not* rounding it.
What kind of misquoting are you doing? My response was about Math.round() NOT TOFIXED. MY RESPONSE WAS AS FOLLOWS:
Hey Dennis, you need to lay off the coffee for a while. There wasn't any
misquoting, he was simply talking at a slight tangent.
--
--
Fabian
Visit my website often and for long periods! http://www.lajzar.co.uk
Here is an actual code example of the formula defined by Dennis Marks.
function number_ClipToTheHundredth(value) {
return Math.round(parseFloat(value) * 100)/100;
}
Change the 100 to any decimal place you want, 10, 100, 1000 (10^x).
Mike
"Allen Thompson" <ge********@mindspring.com> wrote in message
news:Lx*************@newsread2.news.atl.earthlink. net... Is there a script that will round off a number to a certain number of decimal places? -Allen Thompson
Mike hu kiteb: Here is an actual code example of the formula defined by Dennis Marks.
function number_ClipToTheHundredth(value) { return Math.round(parseFloat(value) * 100)/100; }
Change the 100 to any decimal place you want, 10, 100, 1000 (10^x).
To avoid floating errors, it is wise to leave the final divisor off
until you are ready to display the number. Wherver possible, it is best
to use whole numbers in javascript.
--
--
Fabian
Visit my website often and for long periods! http://www.lajzar.co.uk
"Fabian" <la****@hotmail.com> wrote in news:brgqee$33vjm$2@ID-
174912.news.uni-berlin.de: To avoid floating errors, it is wise to leave the final divisor off until you are ready to display the number. Wherver possible, it is best to use whole numbers in javascript.
Or any language in which arithmetic is done in floating point. For
example, when doing financial calculations, the rule is always to convert
all the numbers you use into the smallest units you do business with (for
ordinary commerce, that would be cents or your country's equivalent) and
only convert to larger units (e.g. dollars) at the very end.
There's an entire branch of mathematics (numerical analysis) devoted to the
study of the consequences of dealing with concrete representations of
numbers rather than ideal abstract numbers. When you study numerical
analysis you learn, for example, that if you try to subtract one large
floating-point number from another larger floating-point number that's
nearly equal to it, you cand lose nearly all of your significant digits
(which means, among other things, that the textbook formula for single-pass
computation of the standard deviation of a set of data often fails when
implemented on a computer). And that if you try to compare two floating-
point numbers for exact equality, you often get surprising results. Try:
for (i=0;i<=10;i+=0.1) {document.write(i+"<br>");}
and you'll find that the last value displayed isn't exactly ten. Floating-
point numbers are *approximations* to real numbers. Floating-point
addition, for example, isn't strictly associative even though real-number
addition is.
JRS: In article <br**********@sparta.btinternet.com>, seen in
news:comp.lang.javascript, bagbourne <no***@noway.com> posted at Sat, 13
Dec 2003 18:50:43 :- In article <Lx*************@newsread2.news.atl.earthlink.net> , Allen Thompson <ge********@mindspring.com> wrote:
Is there a script that will round off a number to a certain number of decimal places? -Allen Thompson
or use the toFixed() method of the Number object:
http://devedge.netscape.com/library/...ference/number .html#1193137
Method toFixed() should not be used on the Internet. Implementations
are buggy, and the method is not available on all browsers currently in
use.
It is wise to understand the newsgroup FAQ before answering here.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.
JRS: In article <13*************************@dcsi.net>, seen in
news:comp.lang.javascript, Dennis M. Marks <de******@dcsi.net> posted at
Sat, 13 Dec 2003 16:34:30 :- The original question was: Is there a script that will round off a number to a certain number of decimal places? -Allen Thompson
It did not state "display" a certain number of decimal places. My answer is mathematically correct. To show trailing zeros after the decimal point text zeros would have to be affixed to the end. It's too bad that javascript doesn't have output formatting like some other languages.
Objects of type Number do not have decimal places. They are binary,
with 52 binary places after an implicit "1.", and an exponent.
Therefore, your code does not do the impossible function that is asked
for.
Agreed that javascript should have had proper number-to-string
formatting built in; however, please post here if there is any
reasonable formatting required that cannot be found by using the
newsgroup FAQ.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.
@SM wrote: bagbourne a ecrit : http://devedge.netscape.com/library/...r.html#1193137 very interesting they send me to a page to tell me to download NN7 beuark ! :-(((
They do not. Tested with Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.0; Q312461).
Besides, JavaScript 1.5 in implemented in Mozilla/5.0 which
only *includes* Netscape 6+.
--
<prayer_wheel> The trailing space is missing. </prayer_wheel>
PointedEars
JRS: In article <Y4********************@comcast.com>, seen in
news:comp.lang.javascript, Mike <mi*****************@synovic.com> posted
at Sat, 13 Dec 2003 20:52:30 :- Here is an actual code example of the formula defined by Dennis Marks.
function number_ClipToTheHundredth(value) { return Math.round(parseFloat(value) * 100)/100; }
Change the 100 to any decimal place you want, 10, 100, 1000 (10^x).
For what purpose do you call parseFloat? The OP wishes to start with a
number.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.
JRS: In article <Xn******************************@130.133.1.4>, seen in
news:comp.lang.javascript, Eric Bohlman <eb******@earthlink.net> posted
at Sun, 14 Dec 2003 06:20:05 :- "Fabian" <la****@hotmail.com> wrote in news:brgqee$33vjm$2@ID- 174912.news.uni-berlin.de:
To avoid floating errors, it is wise to leave the final divisor off until you are ready to display the number. Wherver possible, it is best to use whole numbers in javascript. Or any language in which arithmetic is done in floating point. For example, when doing financial calculations, the rule is always to convert all the numbers you use into the smallest units you do business with (for ordinary commerce, that would be cents or your country's equivalent) and only convert to larger units (e.g. dollars) at the very end.
No, that is not always the rule. The rule used (after the fixing of
parities) for converting the currencies which are now replaced by the
Euro, to the Euro and among each other, was rigorously specified, and
was different. ISTM likely that a similar rule will be used when other
countries join.
There's an entire branch of mathematics (numerical analysis) devoted to the study of the consequences of dealing with concrete representations of numbers rather than ideal abstract numbers. When you study numerical analysis you learn, for example, that if you try to subtract one large floating-point number from another larger floating-point number that's nearly equal to it,
I hope that most would have learned the results of such subtraction long
before studying numerical analysis.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk / ??*********@physics.org ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)
Thomas 'PointedEars' Lahn a ecrit : @SM wrote:
bagbourne a ecrit : http://devedge.netscape.com/library/...r.html#1193137 very interesting they send me to a page to tell me to download NN7 beuark ! :-(((
They do not.
They Did !
tested with NC 4.5 (French on Mac PPC system 8.6)
Tested with Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461).
--
************************************************** ************
Stéphane MORIAUX : mailto:st*********************@wanadoo.fr
@SM wrote: Thomas 'PointedEars' Lahn a ecrit : @SM wrote: > bagbourne a ecrit : >> http://devedge.netscape.com/library/...r.html#1193137 > > very interesting > they send me to a page to tell me to download NN7 > beuark ! :-((( They do not.
They Did ! tested with NC 4.5 (French on Mac PPC system 8.6)
And with Mozilla/4.8 [en] (Windows NT 5.0; U) and below.
Well, just click the Back button or the "Continue using DevEdge with
this browser" link and the info document will not appear again. They
are right to drop you a note that you are using a buggy and outdated
browser.
--
.....^
When will you ever learn? :-(
PointedEars
Thomas 'PointedEars' Lahn a ecrit : @SM wrote:
Thomas 'PointedEars' Lahn a ecrit : @SM wrote: > bagbourne a ecrit : >> http://devedge.netscape.com/library/...r.html#1193137 > > very interesting > they send me to a page to tell me to download NN7 Well, just click the Back button or the "Continue using DevEdge with this browser" link and the info document will not appear again. They are right to drop you a note that you are using a buggy and outdated browser.
Fastidious !
Because I dont know my browser is old ?
What they do for I can use a "new" browser ? :-(((((
Because Mozilla 5 is unuseful on my machine.
-- ....^ When will you ever learn? :-(
PointedEars
Beter isn't without signature ;-))
(for reply url, I'm working about it, let me some time) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ron Adam |
last post by:
With all the discussion about numbers here and particularly in regards
to prePEP concerning decimal data types got me wondering about how
numbers are handled when it comes to rounding. And if that...
|
by: Christopher T King |
last post by:
Speakign of rounding errors, why is round() defined to round away from
zero? I've always been quite fond of the mathematical definition of
rounding of floor(n+.5). As a contrived example of why the...
|
by: Caesar Augustus |
last post by:
Hello Tom,
Long time listener, first time caller...
I have been working with vb code to automate a salary increase process
that not only rounds to the nearest penny but also rounds to the...
|
by: dhildebrandt |
last post by:
I have a query that uses the Round function to change decimals into
whole numbers. For 4 years straight the thing has always rounded the
numbers in exactly the same way so that whenever I update...
|
by: Norvin Laudon |
last post by:
Hi,
Can somebody explain the following, from the MSDN documentation for the
"System.Convert.ToInt32(double)" function
<quote>
Return Value
value rounded to the nearest 32-bit signed...
|
by: Joe Attardi |
last post by:
Hi all,
Math is not my strongest area so forgive me if I use some of the wrong
terminology.
It seems that scientific notation is immune to rounding errors. For
example:
(4.98 * 100) + 5.51 ...
|
by: cj |
last post by:
Lets assume all calculations are done with decimal data types so things
are as precise as possible.
When it comes to the final rounding to cut a check to pay dividends for
example in VB rounding...
|
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: md |
last post by:
Hi
Does any body know, how to round a double value with a specific number
of digits after the decimal points?
A function like this:
RoundMyDouble (double &value, short numberOfPrecisions)
...
|
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: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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: 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,...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |