FormCalc Bug? Calculating total Time and Rounding to the nearest Tenth

I am working on a timesheet using LiveCycle designer ES2. I am using FormCalc as my authoring script, but I am running into a problem with some of the “total time” rounding correctly to the tenth decimal point.

Here is an example:
Time In = 8:00 am
Time Out = 11:09 am = 3.15*
* When the sum is rounded, it rounds to 3.1, but it should be 3.2. This error does not always occur when the second number is 5.

I have ran a couple of test and these are my results:
8:00 – 11:09 = 3.15 (rounding to 3.1)
8:00 – 11:21 = 3.35 (rounding to 3.3)
8:00 – 11:27 = 3.45 (rounding to 3.5) – correct
8:00 – 11:33 = 3:55 (rounding to 3.6) – correct
8:00 – 11:39 = 3:65 (rounding to 3.7) - correct
8:00 – 11:45 = 3:75 (rounding to 3.8) - correct
8:00 – 11:51 = 3.85 (rounding to 3.8)
8:00 – 11:57 = 3.95 (rounding to 3.9)

These are the patterns I am using for Display: “num{zz9.9}” – Data pattern is “num{zz9.99}”

I also need help writing some script (either FormCalc or JavaScript)… I have a numeric field which shows the “Total No. of Hours” completed in a week (sum of "time worked/holidays/vacation" = 40 hours or less). The number of hours worked over 40 needs to appear in the next field for “Overtime Hours” or “Comp time” earned. I am sure this is an easy script, but I am new to FormCalc and JavaScript so any help would be greatly appreciated.

Jenn


Jennifer Gallini


3 Answers

Hi,

you're mixing several field types, which is not the best solution.
For time calculations you should use date/time fields instead of numeric fields.
These fields can use patterns for times such as time{"HH:MM"}.

To calculate times use the FormCalc functions Num2Time and Time2Num.

$ = Abs(Time2Num(TimeOut.formattedValue, "HH:MM") - Time2Num(TimeIn.formattedValue, "HH:MM")) / (60 * 60 * 1000)


Marcus Radzuweit   

This is my calculations (very similiar to your response). What I am doing wrong? The date fields are formated to Date patterns and the "total hours worked" column is formated to numeric field.

Here is my script:

topmostSubform.detail.tot1::calculate - (FormCalc, client)

var Time1 = 0

if (HasValue(pmIn) and HasValue(pmOut)) then

Time1 = Time2Num(pmOut.formattedValue, "h:MM A") - Time2Num(pmIn.formattedValue, "h:MM A")

endif

// compute total time in hours from the millisecond value

$.rawValue = Sum(Time1)/3600000


Jennifer Gallini   

Ok,

two solutions:
1: When you want to display the difference in decimal numbers such as 1,75 hours.

if (HasValue(pmIn) and HasValue(pmOut)) then$ = Abs(Time2Num(pmIn.formattedValue, "HH:MM") - Time2Num(pmOut.formattedValue, "HH:MM")) / 3600000endif

2. When you want the difference being displayed in the time format such as 01:45 hours.

if (HasValue(pmIn) and HasValue(pmOut)) then$ = Num2Time(Abs(Time2Num(pmIn.formattedValue, "HH:MM") - Time2Num(pmOut.formattedValue, "HH:MM")) / 3600000 + 1, "HH:MM")endif


Marcus Radzuweit   


Please specify a reason: