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

have a problem with javascript caculate

P: n/a
Hi all,
my script:
<script>
var t= 2.2 - 1.5; // t = 0.7
alert(t);
</script>
but result's alert: 0.7000000000000000002
Help me!

Aug 14 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
w2ajax wrote on 14 aug 2007 in comp.lang.javascript:
Hi all,
my script:
<script>
var t= 2.2 - 1.5; // t = 0.7
alert(t);
</script>
but result's alert: 0.7000000000000000002
Help me!
Javascript is correct, your assumpltion is wrong.
http://www.jibbering.com/faq/#FAQ4_7
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 14 '07 #2

P: n/a
w2ajax wrote:
<script>
http://validator.w3.org/
var t= 2.2 - 1.5; // t = 0.7
alert(t);
</script>
but result's alert: 0.7000000000000000002
http://www.jibbering.com/faq/#FAQ4_7
Aug 14 '07 #3

P: n/a
how about:

var t = 2.2 - 1.5;
t = t.toFixed( 1 );

http://developer.mozilla.org/en/docs...:Number_Object

Aug 14 '07 #4

P: n/a
On Aug 14, 4:10 pm, "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>
wrote:
how about:

var t = 2.2 - 1.5;
t = t.toFixed( 1 );

http://developer.mozilla.org/en/docs...5_Guide:Predef...
But now I'm reading about JScript's toFixed bug.

toFixed

The javascript internal method Number.toFixed() is only available in
later browsers, and even then will not always be suitable. The
internal method may err : try 0.07 and 1.129 for possible truncation;
try toFixed(0) for 0.50 to 0.94 or to 0.999...

" ... bug in Number.toFixed(), namely that for values n in
{(-0.94,-0.5], [0.5,0.94)}, n.toFixed(0) returns 0 instead of -1 or
1."

Apparently, (0.07).toFixed(1) gives 0.0.

http://www.merlyn.demon.co.uk/js-rndg1.htm

I'm always continually amazed at what A piece of junk IE is.

Garrett

Aug 14 '07 #5

P: n/a
Dr J R Stockton said the following on 8/16/2007 12:48 PM:
In comp.lang.javascript message <I5********************@giganews.com>,
Tue, 14 Aug 2007 21:32:33, Randy Webb <Hi************@aol.composted:
>> Apparently, (0.07).toFixed(1) gives 0.0.
It is also covered in the group FAQ although it is not in an entry
dedicated to toFixed (It is in 4.6).

It is a little hard to spot, though, because in that section code-in-
text is not marked with <CODE>.

The code in the code-box of that section is somewhat different from what
I would presently recommend.
I believe that I have asked in the past when you make such statements
about code not being what you would recommend what you would recommend
and you never give a straight answer.

What code would you recommend to replace whatever code is in 4.6?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 16 '07 #6

P: n/a
In comp.lang.javascript message <_r*********************@giganews.com>,
Thu, 16 Aug 2007 14:46:59, Randy Webb <Hi************@aol.composted:
>Dr J R Stockton said the following on 8/16/2007 12:48 PM:
>In comp.lang.javascript message <I5********************@giganews.com>,
Tue, 14 Aug 2007 21:32:33, Randy Webb <Hi************@aol.composted:
>>> Apparently, (0.07).toFixed(1) gives 0.0.
It is also covered in the group FAQ although it is not in an entry
dedicated to toFixed (It is in 4.6).
It is a little hard to spot, though, because in that section code-
in-
text is not marked with <CODE>.
The code in the code-box of that section is somewhat different from
what
I would presently recommend.

I believe that I have asked in the past when you make such statements
about code not being what you would recommend what you would recommend
and you never give a straight answer.

What code would you recommend to replace whatever code is in 4.6?
The code which the obvious link in that section of the FAQ leads to, of
course. That will always give my currently-recommended code (as long as
the site survives). That includes preselected code tests, a test form
in which one can choose whatever arguments one wishes for each of the
functions I have in the code, and one in which a user-supplied function
can be correspondingly tested.
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 17 '07 #7

P: n/a
Dr J R Stockton said the following on 8/17/2007 8:06 AM:
In comp.lang.javascript message <_r*********************@giganews.com>,
Thu, 16 Aug 2007 14:46:59, Randy Webb <Hi************@aol.composted:
>Dr J R Stockton said the following on 8/16/2007 12:48 PM:
>>In comp.lang.javascript message <I5********************@giganews.com>,
Tue, 14 Aug 2007 21:32:33, Randy Webb <Hi************@aol.composted:

Apparently, (0.07).toFixed(1) gives 0.0.
It is also covered in the group FAQ although it is not in an entry
dedicated to toFixed (It is in 4.6).
It is a little hard to spot, though, because in that section code-
in-
text is not marked with <CODE>.
The code in the code-box of that section is somewhat different from
what
I would presently recommend.
I believe that I have asked in the past when you make such statements
about code not being what you would recommend what you would recommend
and you never give a straight answer.

What code would you recommend to replace whatever code is in 4.6?

The code which the obvious link in that section of the FAQ leads to, of
course. That will always give my currently-recommended code (as long as
the site survives).
I have no intentions of checking your site daily/weekly to see if you
have updated code that you would like to see in the FAQ. If the code in
the FAQ needs to be updated then you can post it here, it gets reviewed,
and then into the FAQ. Otherwise, the FAQ will stay the way it is.

I can see why Richard ignored most of your requests for changes. You
don't offer any changes, you simply say "That should be changed".

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
FAQ Notes: http://www.jibbering.com/faq/faq_notes/faq_notes.html
ECMAScript Language Specification via FAQ2.6
Aug 21 '07 #8

P: n/a
In comp.lang.javascript message <de********************@giganews.com>,
Mon, 20 Aug 2007 22:04:53, FAQEditor <cl****@comcast.netposted:
>I have no intentions of checking your site daily/weekly to see if you
have updated code that you would like to see in the FAQ.
But when I tell you that the code in a section of the FAQ is out-of-
date, and that section of the FAQ cites a page in my site, you should
not find it too difficult to deduce when and where to look.
>I can see why Richard ignored most of your requests for changes. You
don't offer any changes, you simply say "That should be changed".
When there is an answer that the FAQ fails to give, and it is an answer
that I would like to know, I can hardly be expected to provide it.

Don't try and excuse yourself by reference to another failed FAQ
maintainer; the recent list of changes is derisory.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zipTimo Salmi's Turbo Pascal FAQ.
Aug 21 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.