473,396 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

have a problem with javascript caculate

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
8 1320
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
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
how about:

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

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

Aug 14 '07 #4
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Yaron Cohen | last post by:
Hi, I would like to ask for you help. I have a page that contains few JS files (please see below). The problem is that sometimes one of the files is not loaded (I am using IE 5.5). I get...
4
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following...
3
by: Jamie Pittman via AccessMonster.com | last post by:
I am currently working on a project to take an employees time for a day and and if over 8 hours it would move those extra hours to overtime row amd not caculate into regular time. I started to use...
4
by: Nick | last post by:
hi, guys I don't know where should I put this post, because this is a general question, not really a c# question. The question is how to caculate the real distance between two geographical...
1
by: Agnes | last post by:
My program flow is (1)User click a button to print the account report (2)click button ->> show one dialog progress bar form (3)That form will create a thread ->> thread exexcute 10 sql command to...
18
by: damezumari | last post by:
I would like to know how many of the visitors to my site has js enabled and how many has it turned off. I haven't found a simple solution searching Google groups so I suggest the following using...
2
by: ad | last post by:
There are some uses in my windows 2003 domain. The can print with a printer in a pc. How can I caculate the numbers of pages of a users?
1
by: paulyXvpf | last post by:
Hello javascript folks, PROBLEM: Javascript dropdown problem in IE 6 and IE7 DESCRIPTION: menu falls behind a container box on web page COMMENTS: It works fine in Firefox but not in IE 6/7...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.