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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding money values 1

Status
Not open for further replies.

craigward

Programmer
Nov 13, 2007
230
GB
Hi there,

I am having trouble getting a math function to add to currency values together.

The issue is the rounding, here is a basic example. var c is returning 3761 and it should be 3762.38

I have played with a lot of examples but can't get it working. Is there a simple method out there to achieve the correct calculation?

Thanks for looking.

Code:
<html>
<head>

</head>

<body onLoad="calcvals()">

<script language="JavaScript">

function calcvals(){

var a = '3145,8'
var b = '616,58'
var c = parseFloat(a) + parseFloat(b)
alert(c)

}

</script>

</body>
</html>
 
Javascript has no locale settings, so it can't recognize a comma as a decimal separator. Having values that use a comma as a decimal separator will cause issues like the one you are having. That is the actual numbers are being parsed as rounded integers since the comma there is not valid.

Change your values to use periods instead.

Code:
var a = '3145[COLOR=white red].[/color]8'

var b = '616[COLOR=white red].[/color]58'

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top