Skip navigation
shiny01248
Currently Being Moderated

Calculation errors when entering currency formatted numbers

Apr 25, 2012 2:50 PM

Tags: #javascript #calculated_field

I am creating a form that has several fields, all defined as numbers using currency formatting:

CurrentBase which is the monthly pay rate which the user enters

CurrentYearly which is the annual pay rate and is calculated as CurrentBase * 12 -- this is read-only

CurrentOvertime which is an estimate based on 6% of the CurrentYearly and which the user can override.

 

As a OnBlur event in CurrentBase I use this code:

 

var cYear=this.getField("CurrentYearly");

cYear.value=this.getField("CurrentBase").value* 12;

this.getField("CurrentOvertime").value=cYear.value*0.06;

 

I also have a CurrentTotal field which is the sum of CurrentYearly and CurrentOvertime, set in the custom calculation script for CurrentTotal as:

 

this.getField("CurrentTotal").value=this.getField("CurrentYearly").val ue+this.getField("CurrentOvertime").value;

 

Here is the problem:

 

If I enter a CurrentBase, such as 1234 my script correctly calculates the CurrentYearly ($14,808.00), CurrentOvertime ($888.48) and CurrentTotal ($15,696.48) correctly.

Now if I override the CurrentOvertime using something like 555.00 the total calculates correctly, in this case $15,363.00

But if I type 555. (omitting the decimal values) my CurrentTotal displays as $14,808,555.00

 

This seems to be a bug.  Any thoughts on why and how to fix it.

Bob

 
Replies
  • Currently Being Moderated
    Apr 25, 2012 3:08 PM   in reply to shiny01248

    The value is treated as a string instead of a number, so it is just being

    concatenated to the other value, instead of added to it.

    The solution is to explicitly convert the value to a number, which can be

    done in several ways. The simplest is to add a plus symbol in front of it,

    like so (the brackets are not required, but they make it easier to read):

    this.getField("CurrentTotal").value =

    (+this.getField("CurrentYearly").value) +

    (+this.getField("CurrentOvertime").value);

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points