Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-30-2004, 09:38 AM   #1 (permalink)
arwilliams
Registered User
 
Join Date: Sep 2004
Posts: 2
arwilliams is on a distinguished road
Angry adding numbers doesn't add up !!! help

Ok I am a complete newbie, so remember we all start somewhere!

I am trying to add three numbers together, but it never gives me what I want. My numbers where originally coming from variables so I thought perhaps there was something wrong with how i had set it up. So I hardcoded them and simplified it to the following :


<script language="JavaScript" type="text/JavaScript">
alert (494.06 + 576.26 + 514.06)
</script>

The answer I get is : 1584.3799999999998

What is going on ????????

Many thanks
arwilliams is offline   Reply With Quote
Old 09-30-2004, 09:40 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
that looks right to me. what do you want it to return?
__________________
Mike
sde is offline   Reply With Quote
Old 09-30-2004, 09:45 AM   #3 (permalink)
arwilliams
Registered User
 
Join Date: Sep 2004
Posts: 2
arwilliams is on a distinguished road
I am dealing with currency so I need 1584.38.

I am assuming its something to do with the way numbers are dealt with by javascript?
arwilliams is offline   Reply With Quote
Old 09-30-2004, 10:17 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
toFixed(2) =)
Code:
<script langauge=javascript>
var i = 494.06 + 576.26 + 514.06;
alert("before toFixed(2): "+i);
i = i.toFixed(2);
alert("after toFixed(2): "+i);
alert("all in one line: "+(494.06 + 576.26 + 514.06).toFixed(2) );
</script>
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding Variable Numbers falsepride HTML, XML, Javascript, AJAX 1 10-28-2004 01:23 PM
Adding infinite text fields Mkers12 Java 8 08-17-2004 12:13 PM
How to add an error statement w00t Standard C, C++ 4 08-13-2002 10:54 PM
adding to an array in php sde PHP 1 06-12-2002 11:04 AM


All times are GMT -8. The time now is 10:42 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting