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

# Accurate Rounding of Hours to Hundredth Decimal

New Here ,
Nov 18, 2022 Nov 18, 2022

Copied

Greetings,

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

TOPICS
JavaScript , PDF forms

Views

275

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 Expert , Nov 18, 2022 Nov 18, 2022

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

Community Expert ,
Nov 18, 2022 Nov 18, 2022

Copied

LATEST

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