By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,599 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,599 IT Pros & Developers. It's quick & easy.

rounding off numbers

P: n/a
Is there a script that will round off a number to a certain number of
decimal places? -Allen Thompson
Jul 20 '05 #1
Share this Question
Share on Google+
22 Replies


P: n/a
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! =-----
Jul 20 '05 #2

P: n/a
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

Jul 20 '05 #3

P: n/a
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
Jul 20 '05 #4

P: n/a
@SM
bagbourne a ecrit :
or use the toFixed() method of the Number object:

http://devedge.netscape.com/library/...r.html#1193137


very interesting
they send me to a page to tell me to download NN7
beuark ! :-(((

--
************************************************** ************
Stéphane MORIAUX : mailto:st*********************@wanadoo.fr
Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)
http://perso.wanadoo.fr/stephanePOINTmoriaux/internet/
************************************************** ************
Jul 20 '05 #5

P: n/a
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! =-----
Jul 20 '05 #6

P: n/a
@SM
"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/
************************************************** ************
Jul 20 '05 #7

P: n/a
"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.
Jul 20 '05 #8

P: n/a
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! =-----
Jul 20 '05 #9

P: n/a
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.
Jul 20 '05 #10

P: n/a
@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")
Jul 20 '05 #11

P: n/a
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

Jul 20 '05 #12

P: n/a
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

Jul 20 '05 #13

P: n/a
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

Jul 20 '05 #14

P: n/a
"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.
Jul 20 '05 #15

P: n/a
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.
Jul 20 '05 #16

P: n/a
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.
Jul 20 '05 #17

P: n/a
@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
Jul 20 '05 #18

P: n/a
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.
Jul 20 '05 #19

P: n/a
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)
Jul 20 '05 #20

P: n/a
@SM
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

Jul 20 '05 #21

P: n/a
@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
Jul 20 '05 #22

P: n/a
@SM


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)
Jul 20 '05 #23

This discussion thread is closed

Replies have been disabled for this discussion.