Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I have to add my thanks and appreciation for your wonderful site... People who frequent the site are the two best things - nice and smart..."

Geography

Where in the world do Tek-Tips members come from?
craigward (Programmer)
4 Jul 12 11:57
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> 
Helpful Member!  vacunita (Programmer)
4 Jul 12 12:21
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.8'

var b = '616.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 &amp; Tech

craigward (Programmer)
4 Jul 12 12:26
Thank you smile

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close