Connecting Tech Pros Worldwide Forums | Help | Site Map

rounding off numbers

Allen Thompson
Guest
 
Posts: n/a
#1: Jul 20 '05
Is there a script that will round off a number to a certain number of
decimal places? -Allen Thompson



Dennis M. Marks
Guest
 
Posts: n/a
#2: Jul 20 '05

re: rounding off numbers


In article <LxGCb.99$sW5.50@newsread2.news.atl.earthlink.net> , Allen
Thompson <genericjoe@mindspring.com> wrote:
[color=blue]
> Is there a script that will round off a number to a certain number of
> decimal places? -Allen Thompson
>
>[/color]

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! =-----
bagbourne
Guest
 
Posts: n/a
#3: Jul 20 '05

re: rounding off numbers


Dennis M. Marks wrote:[color=blue]
> In article <LxGCb.99$sW5.50@newsread2.news.atl.earthlink.net> , Allen
> Thompson <genericjoe@mindspring.com> wrote:
>
>[color=green]
>>Is there a script that will round off a number to a certain number of
>>decimal places? -Allen Thompson
>>
>>[/color]
>
>
> 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.
>[/color]

or use the toFixed() method of the Number object:

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

HikksNotAtHome
Guest
 
Posts: n/a
#4: Jul 20 '05

re: rounding off numbers


In article <brfn22$oqs$1@sparta.btinternet.com>, bagbourne <noway@noway.com>
writes:
[color=blue]
>Dennis M. Marks wrote:[/color]

<--snip-->
[color=blue]
>
>or use the toFixed() method of the Number object:[/color]

alert((0.007).toFixed(2)
//gives 0.00

Which is dropping it to two decimal places, *not* rounding it.



--
Randy
@SM
Guest
 
Posts: n/a
#5: Jul 20 '05

re: rounding off numbers


bagbourne a ecrit :
[color=blue]
> or use the toFixed() method of the Number object:
>
> http://devedge.netscape.com/library/...r.html#1193137[/color]

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

--
************************************************** ************
Stéphane MORIAUX : mailto:stephaneOTER-MOImoriaux@wanadoo.fr
Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)
http://perso.wanadoo.fr/stephanePOINTmoriaux/internet/
************************************************** ************


Dennis M. Marks
Guest
 
Posts: n/a
#6: Jul 20 '05

re: rounding off numbers


In article <20031213155410.11891.00002906@mb-m11.aol.com>,
HikksNotAtHome <hikksnotathome@aol.com> wrote:
[color=blue]
> In article <brfn22$oqs$1@sparta.btinternet.com>, bagbourne <noway@noway.com>
> writes:
>[color=green]
> >Dennis M. Marks wrote:[/color]
>
> <--snip-->
>[color=green]
> >
> >or use the toFixed() method of the Number object:[/color]
>
> alert((0.007).toFixed(2)
> //gives 0.00
>
> Which is dropping it to two decimal places, *not* rounding it.[/color]


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! =-----
@SM
Guest
 
Posts: n/a
#7: Jul 20 '05

re: rounding off numbers


"Dennis M. Marks" a ecrit :
[color=blue]
> 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.[/color]

and ???

x = Math.round(2*1000)/1000;
gives
x = 2
where are the missing .000 to obtain 2.000

--
************************************************** ************
Stéphane MORIAUX : mailto:stephaneOTER-MOImoriaux@wanadoo.fr
Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)
http://perso.wanadoo.fr/stephanePOINTmoriaux/internet/
************************************************** ************


Richard Cornford
Guest
 
Posts: n/a
#8: Jul 20 '05

re: rounding off numbers


"Dennis M. Marks" <denmarks@dcsi.net> wrote in message
news:131220031506178104%denmarks@dcsi.net...[color=blue]
>In article <20031213155410.11891.00002906@mb-m11.aol.com>,
>HikksNotAtHome <hikksnotathome@aol.com> wrote:
>[color=green]
>>In article <brfn22$oqs$1@sparta.btinternet.com>,
>>bagbourne <noway@noway.com> writes:
>>[color=darkred]
>>>Dennis M. Marks wrote:[/color]
>>
>><--snip-->[color=darkred]
>>>
>>>or use the toFixed() method of the Number object:[/color]
>>
>>alert((0.007).toFixed(2)
>>//gives 0.00
>>
>>Which is dropping it to two decimal places, *not*
>>rounding it.[/color]
>
>What kind of misquoting are you doing? ...[/color]
<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.


Dennis M. Marks
Guest
 
Posts: n/a
#9: Jul 20 '05

re: rounding off numbers


In article <3FDBA9A9.E426D870@wanadoo.fr>, <"@SM"> wrote:
[color=blue]
> "Dennis M. Marks" a ecrit :
>[color=green]
> > 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.[/color]
>
> and ???
>
> x = Math.round(2*1000)/1000;
> gives
> x = 2
> where are the missing .000 to obtain 2.000
>[/color]
<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! =-----
Dr John Stockton
Guest
 
Posts: n/a
#10: Jul 20 '05

re: rounding off numbers


JRS: In article <131220030825398921%denmarks@dcsi.net>, seen in
news:comp.lang.javascript, Dennis M. Marks <denmarks@dcsi.net> posted at
Sat, 13 Dec 2003 08:25:39 :-[color=blue]
>In article <LxGCb.99$sW5.50@newsread2.news.atl.earthlink.net> , Allen
>Thompson <genericjoe@mindspring.com> wrote:
>[color=green]
>> Is there a script that will round off a number to a certain number of
>> decimal places?[/color][/color]

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.

[color=blue]
>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.[/color]

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.
Michael Winter
Guest
 
Posts: n/a
#11: Jul 20 '05

re: rounding off numbers


@SM wrote on 14 Dec 2003 at Sun, 14 Dec 2003 00:07:11 GMT:
[color=blue]
> x = Math.round(2*1000)/1000;
> gives
> x = 2
> where are the missing .000 to obtain 2.000[/color]

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.Winter@blueyonder.co.invalid (replace ".invalid" with ".uk")
Fabian
Guest
 
Posts: n/a
#12: Jul 20 '05

re: rounding off numbers


Dennis M. Marks hu kiteb:
[color=blue][color=green]
>> Which is dropping it to two decimal places, *not* rounding it.[/color]
>
>
> What kind of misquoting are you doing? My response was about
> Math.round() NOT TOFIXED. MY RESPONSE WAS AS FOLLOWS:[/color]


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

Mike
Guest
 
Posts: n/a
#13: Jul 20 '05

re: rounding off numbers


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" <genericjoe@mindspring.com> wrote in message
news:LxGCb.99$sW5.50@newsread2.news.atl.earthlink. net...[color=blue]
> Is there a script that will round off a number to a certain number of
> decimal places? -Allen Thompson
>
>[/color]


Fabian
Guest
 
Posts: n/a
#14: Jul 20 '05

re: rounding off numbers


Mike hu kiteb:
[color=blue]
> 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).[/color]

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

Eric Bohlman
Guest
 
Posts: n/a
#15: Jul 20 '05

re: rounding off numbers


"Fabian" <lajzar@hotmail.com> wrote in news:brgqee$33vjm$2@ID-
174912.news.uni-berlin.de:
[color=blue]
> 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.[/color]

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.
Dr John Stockton
Guest
 
Posts: n/a
#16: Jul 20 '05

re: rounding off numbers


JRS: In article <brfn22$oqs$1@sparta.btinternet.com>, seen in
news:comp.lang.javascript, bagbourne <noway@noway.com> posted at Sat, 13
Dec 2003 18:50:43 :-
[color=blue][color=green]
>> In article <LxGCb.99$sW5.50@newsread2.news.atl.earthlink.net> , Allen
>> Thompson <genericjoe@mindspring.com> wrote:
>>
>>[color=darkred]
>>>Is there a script that will round off a number to a certain number of
>>>decimal places? -Allen Thompson[/color][/color][/color]
[color=blue]
>or use the toFixed() method of the Number object:
>
>http://devedge.netscape.com/library/...ference/number
>.html#1193137[/color]

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.
Dr John Stockton
Guest
 
Posts: n/a
#17: Jul 20 '05

re: rounding off numbers


JRS: In article <131220031634301133%denmarks@dcsi.net>, seen in
news:comp.lang.javascript, Dennis M. Marks <denmarks@dcsi.net> posted at
Sat, 13 Dec 2003 16:34:30 :-[color=blue]
>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.[/color]

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.
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#18: Jul 20 '05

re: rounding off numbers


@SM wrote:
[color=blue]
> bagbourne a ecrit :[color=green]
>> http://devedge.netscape.com/library/...r.html#1193137[/color]
>
> very interesting
> they send me to a page to tell me to download NN7
> beuark ! :-((([/color]

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+.
[color=blue]
> --[/color]

<prayer_wheel> The trailing space is missing. </prayer_wheel>


PointedEars
Dr John Stockton
Guest
 
Posts: n/a
#19: Jul 20 '05

re: rounding off numbers


JRS: In article <Y4-dnUHr0aPtTUaiRVn-jg@comcast.com>, seen in
news:comp.lang.javascript, Mike <mike{removeToemail}@synovic.com> posted
at Sat, 13 Dec 2003 20:52:30 :-[color=blue]
>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).[/color]

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.
Dr John Stockton
Guest
 
Posts: n/a
#20: Jul 20 '05

re: rounding off numbers


JRS: In article <Xns9451571F1CA0ebohlmanomsdevcom@130.133.1.4>, seen in
news:comp.lang.javascript, Eric Bohlman <ebohlman@earthlink.net> posted
at Sun, 14 Dec 2003 06:20:05 :-[color=blue]
>"Fabian" <lajzar@hotmail.com> wrote in news:brgqee$33vjm$2@ID-
>174912.news.uni-berlin.de:
>[color=green]
>> 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.[/color]
>
>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.[/color]

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.

[color=blue]
>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,[/color]

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 / ??.Stockton@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)
@SM
Guest
 
Posts: n/a
#21: Jul 20 '05

re: rounding off numbers


Thomas 'PointedEars' Lahn a ecrit :
[color=blue]
> @SM wrote:
>[color=green]
> > bagbourne a ecrit :[color=darkred]
> >> http://devedge.netscape.com/library/...r.html#1193137[/color]
> >
> > very interesting
> > they send me to a page to tell me to download NN7
> > beuark ! :-((([/color]
>
> They do not.[/color]

They Did !
tested with NC 4.5 (French on Mac PPC system 8.6)

[color=blue]
> Tested with Mozilla/4.0 (compatible; MSIE 6.0;
> Windows NT 5.0; Q312461).[/color]

--
************************************************** ************
Stéphane MORIAUX : mailto:stephaneOTER-MOImoriaux@wanadoo.fr

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#22: Jul 20 '05

re: rounding off numbers


@SM wrote:
[color=blue]
> Thomas 'PointedEars' Lahn a ecrit :[color=green]
>> @SM wrote:[color=darkred]
>> > 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 ! :-((([/color]
>>
>> They do not.[/color]
>
> They Did !
> tested with NC 4.5 (French on Mac PPC system 8.6)[/color]

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.
[color=blue]
> --[/color]
.....^
When will you ever learn? :-(


PointedEars
@SM
Guest
 
Posts: n/a
#23: Jul 20 '05

re: rounding off numbers




Thomas 'PointedEars' Lahn a ecrit :[color=blue]
>
> @SM wrote:
>[color=green]
> > Thomas 'PointedEars' Lahn a ecrit :[color=darkred]
> >> @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[/color][/color][/color]
[color=blue]
>
> 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.[/color]

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.

[color=blue][color=green]
> > --[/color]
> ....^
> When will you ever learn? :-(
>
> PointedEars[/color]

Beter isn't without signature ;-))
(for reply url, I'm working about it, let me some time)
Closed Thread


Similar JavaScript / Ajax / DHTML bytes