Skip to main content
Participant
November 18, 2022
Answered

Accurate Rounding of Hours to Hundredth Decimal

  • November 18, 2022
  • 1 reply
  • 552 views

Greetings,

 

(Using Adobe Acrobat Pro)

 

I am currently facing an issue where my calculation of time difference functions however it does not recognize the need to round. For example, in the image below the Hours of 1.16 should be 1.17 - the same is for the 0.41 Hours needing to be 0.42.

 

Thank you in advance for any assistance!

 

This is the code I am using in the Custom Calculation Scripts of each Hours field:

var start = this.getField("Start Time").valueAsString;
var finish = this.getField("End Time").valueAsString;

if (start=="" || finish=="") event.value = "";
else {
	var startArr = start.split(":") ;
	var finishArr = finish.split(":") ;

	var hourDiff = finishArr[0] - startArr[0];
	var minDiff = Math.floor(((finishArr[1] - startArr[1]) / 60)*100);
	if (minDiff<0) {hourDiff--; minDiff += 100;}
	if (hourDiff<0) hourDiff+=24;
	if (minDiff.toString().length == 1) minDiff = '0' + minDiff;
	event.value = hourDiff + "." + minDiff;
}

 

This topic has been closed for replies.
Correct answer try67

Try using Math.round instead of Math.floor, then.

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
November 18, 2022

Try using Math.round instead of Math.floor, then.