Skip to main content
Inspiring
June 6, 2019
Answered

Prevent fractions of minutes showing up in time calculations

  • June 6, 2019
  • 2 replies
  • 1752 views

I have followed the guidelines and examples for creating elapsed time for time sheets where the calculation between time in and time out generates a percentage of an hour, which can be used in calculations for minutes expired. The problem is that the calculation carries unwanted decimals for the minutes as part of an hour.

Example: The difference in time of one minute appears as .02 part of an hour. This field is hidden, and a "minutes" field is shown. The visible portion, after setting the format to Number, with no decimals, appears as "1". However, the real number is "1.2". For single line entries, the result is fine. But when adding more than 5 rows, the .20 of a minute changes the actual total by 5 X .20. Same for other number calculations.

Have tried a number of options found in Forums, but none work in restraining or calculating only the whole numbers. Have worked this out in a spreadsheet program, but unable to find a script that is of help.

Suggestions would be apprciated.

Thanks!

This topic has been closed for replies.
Correct answer lloyd1981

Thanks for posting.

Tried "rounding" in both the field that calculated the fractional part of an hour and the conversion to minutes, both as calculations and part as validations. The final calculated field that must be rounded - the number field that is summed in the total minutes - still carries the decimal amounts.

Need to do more work on which calculation to use in what combination for which result.

The image I posted shows all fields in use - of course, a number of those fields will be hidden in the final product.

The total is not required to be a key element in any financial calculations. It is a simple total and averaging of minutes a first person arriving at a bus stop must wait to board an employee transport. The averages are submitted to the contracted carrier and are not exact, except the sum of the minutes on a sheet can't be more than the actual figures shown. If the hand calculation equals 42 minutes <example> then the form calculation must equal 42 minutes, not 43 or 44.

Just finished an inspection form and a badge inspection security report. Back to this is a few days.

Thanks again.


I was requested to keep both fractions as well as rounding to closest minute. What I did:

Created one field formatted as a number, with 2 decimals. This field calculated the Time entry fields for elapsed time in minutes. Gives a percentage of an hour (did not try to carry beyond 1 hour - not required). Calculation:

var cIn = "In1", cOut = "Out1";  

event.value = TimeDiff("HH:MM", cIn, cOut);

// convert zero entry to null

if(event.value == 0) event.value = "";

In and Out times through 9 entries.

Assigned an arbitrary name: "worked1" [Subsequent fields as "worked2", etc.]

Result of calculation: Example: 12:20 in, 13:00 out = .67 hour

Created a field to convert to minutes with fractions.

Named: "minute1"

Format: None

Calculation (product): 60, [field] worked1

Result: .67 hour = 40.2 (the fraction that drives one crazy)

Field for rounding out fractional values.

Named: "Sum1" ("Sum2", etc. for a total of 9 calculations)

Format: None

Calculation:

event.value = this.getField("minute1").value;

event.value = Math.round((event.value*100)/100) ;

[There is an example of Math.round on net, but posted incorrectly as "event..value"]

Tested through every full minute entry against out and result is valid.

A Total of the Sum fields gives the correct total.

Average time is calculated against a number field (1 for each bus arrival) x Total.

Did not use a group name (In.1, In.2, etc, then calculating on "In").

Thanks to all who visited and offered suggestions.

2 replies

Inspiring
June 6, 2019

How do you want the elapsed time displayed?

lloyd1981Author
Inspiring
June 6, 2019

I responded to trv67.

The result display is not the issue; the underlying amount of the calculation is the issue. One minute difference calculates as .02 of 60 second hour, which results as 1.2.

The script:

var cIn = "TimeIn.0", cOut = "TimeOut.0";  

event.value = TimeDiff("HH:MM", cIn, cOut);

// convert zero entry to null

if(event.value == 0) event.value = "";

for the base calculation. TimeIn and TimeOut become .1, .2, etc for following cells.

Setting the number without decimals does not work because the calculation carries forward so the true number of 1 minute is actually 1.2 for calculation purposes. I can make individual calculations look the way I want - it is the sum of multiple counts that breaks the proper result.

Thanks.

Thom Parker
Community Expert
Community Expert
June 7, 2019

So you want to round the decimal part of the number before it's summed? So you're only summing whole numbers?

event.value = Math.round(TimeDiff("HH:MM", cIn, cOut));

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
June 6, 2019

Can you post your code, or share the actual file with us?

lloyd1981Author
Inspiring
June 6, 2019

Will attempt to attach file. I have brought four other report files up to date  and built others, which require save-as in order to print, etc., but this is one that really requires a redo. The "base" is a copy of a copy and someone tried formatting as number calculations. (Would work if hours had 100 minutes) Before rebuilding the underlying form appearance, I want to make the times report properly.

Have been away from java scripting Acrobat for a couple of years and am rebuilding experience level.

The file is to be reformatted for use with cell phone (works well with iPhone and 5th generation iPad mini) so paper forms and clipboards can be left behind. The attached shows the problem: A sum of the wait times calculates to 8 instead of 7 because of the carried-forward .2 amount of time as a number.

Well, that didn't work. Simple file attaching apparently not supported. Here is a rough shot of the form. When I can figure out how to attach a real file... Guess I could email to trv.

Thanks for responding so soon.

lloyd