• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Accurate Rounding of Hours to Hundredth Decimal

New Here ,
Nov 18, 2022 Nov 18, 2022

Copy link to clipboard

Copied

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;
}

Bill270687883z6d_0-1668799467681.png

 

TOPICS
JavaScript , PDF forms

Views

389

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 18, 2022 Nov 18, 2022

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

Votes

Translate

Translate
Community Expert ,
Nov 18, 2022 Nov 18, 2022

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines