Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with mathematical operation

Status
Not open for further replies.

randomhero69

Technical User
Feb 9, 2004
4
CA
I builded a little script that can calculate the difference of two prices.. The values should have decimals. When I try my function, it returns 479.9600000000003 instead of 479.96 ... Any ideas? Here's the script :

===========================================================

<script language="JavaScript">
var totalPrice = 1139.45;
var articlePrice = 659.49;

function calculateDifference() {
totalPrice = totalPrice - articlePrice;
alert(totalPrice);
}
</script>

<form action="" method="post" name="monForm" id="monForm">

(1139.45 - 659.49)<br>
<br>
I think the answer should be 479.96 but...<br>
<br>
<input type="submit" name="btn" value="Calculate difference" onclick="calculateDifference()">

</form>


===========================================================

Thanks a lot!
 
Code:
Alert(totalPrice.toFixed(2));

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
javascript is case sensitive -> make sure that Alert starts with a lower case a. As an aside, builded is not a word.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
This kinda illustrates that it's no surprise that my replies have been lacking as of late. It makes me wonder why I'm not paid more if this is any indication of my competition.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Thanks to all of you, you helped me a lot on that case! Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top