Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

- Home
- >
- Acrobat SDK
- >
- Rounding / Calc error

Oct 11, 2017

Copy link to clipboard

Copied

I've having some trouble with either rounding or calculating (or both), values for a timesheet.

I thought my coding was all correct, however it seems to be rounding UP.

Example:

**Start time Break Finish time Total hours**

8:30 15 mins 13:15 5.00 ** Total hours should be 4.50**

This is the coding I am using:

var Break = getField("Break1").value;

// start

var start = getField("Start1").value;

var startArr = start.split(":");

// finish

var finish = getField("Finish1").value;

var finishArr = finish.split(":");

//difference

var hourDiff = Math.abs(finishArr[0] - startArr[0]);

var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*100);

if (minDiff.toString().length==1)

minDiff = '0' + minDiff;

var output = hourDiff + "." + minDiff;

if((output==Infinity)||isNaN(output)){

event.value="";

} else {

event.value = output-Break;

}

Could someone please assist where I've gone wrong?

FYI - the break gives the value 0.25

Adobe Community Professional

Replace following lines:

var hourDiff = Math.abs(finishArr[0] - startArr[0]);

var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*100);

with this lines:

var hourDiff = Math.abs(finishArr[0] - startArr[0]);

var minDiff = finishArr[1] - startArr[1];

if ((finishArr[1] - startArr[1]) < 0) {

hourDiff = hourDiff - 1;

minDiff = minDiff + 60;

}

minDiff = Math.floor((minDiff / 60)*100);

TOPICS

Acrobat SDK and JavaScript,
Macintosh,
Windows

Community Guidelines

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

Oct 11, 2017

Copy link to clipboard

Copied

I've having some trouble with either rounding or calculating (or both), values for a timesheet.

I thought my coding was all correct, however it seems to be rounding UP.

Example:

**Start time Break Finish time Total hours**

8:30 15 mins 13:15 5.00 ** Total hours should be 4.50**

This is the coding I am using:

var Break = getField("Break1").value;

// start

var start = getField("Start1").value;

var startArr = start.split(":");

// finish

var finish = getField("Finish1").value;

var finishArr = finish.split(":");

//difference

var hourDiff = Math.abs(finishArr[0] - startArr[0]);

var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*100);

if (minDiff.toString().length==1)

minDiff = '0' + minDiff;

var output = hourDiff + "." + minDiff;

if((output==Infinity)||isNaN(output)){

event.value="";

} else {

event.value = output-Break;

}

Could someone please assist where I've gone wrong?

FYI - the break gives the value 0.25

Adobe Community Professional

Replace following lines:

var hourDiff = Math.abs(finishArr[0] - startArr[0]);

var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*100);

with this lines:

var hourDiff = Math.abs(finishArr[0] - startArr[0]);

var minDiff = finishArr[1] - startArr[1];

if ((finishArr[1] - startArr[1]) < 0) {

hourDiff = hourDiff - 1;

minDiff = minDiff + 60;

}

minDiff = Math.floor((minDiff / 60)*100);

TOPICS

Acrobat SDK and JavaScript,
Macintosh,
Windows

Community Guidelines

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

Clarkopolous

Oct 11, 2017
0
/t5/acrobat-sdk/rounding-calc-error/td-p/9474786
4
Replies
4

Adobe Community Professional
,

Oct 11, 2017

Copy link to clipboard

Copied

Community Guidelines

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

Reply

Loading...

Bernd Alheit

Oct 11, 2017
0
/t5/acrobat-sdk/rounding-calc-error/m-p/9474787#M28263
Oct 12, 2017

Copy link to clipboard

Copied

Would you be able to assist in how I'd go about that? (Coding if possible)

I am racking my brain and cannot think of the correct way to write it.

Thanks in advance

Community Guidelines

Reply

Loading...

Clarkopolous

Oct 12, 2017
0
/t5/acrobat-sdk/rounding-calc-error/m-p/9474788#M28264
Adobe Community Professional
,

Oct 12, 2017

Copy link to clipboard

Copied

Replace following lines:

var hourDiff = Math.abs(finishArr[0] - startArr[0]);

var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*100);

with this lines:

var hourDiff = Math.abs(finishArr[0] - startArr[0]);

var minDiff = finishArr[1] - startArr[1];

if ((finishArr[1] - startArr[1]) < 0) {

hourDiff = hourDiff - 1;

minDiff = minDiff + 60;

}

minDiff = Math.floor((minDiff / 60)*100);

Community Guidelines

Reply

Loading...

Bernd Alheit

Oct 12, 2017
1
/t5/acrobat-sdk/rounding-calc-error/m-p/9474789#M28265
Clarkopolous
LATEST

Oct 16, 2017

Copy link to clipboard

Copied

Thank you so much for your help.

Works perfectly.

Community Guidelines

Reply

Loading...

Clarkopolous

Oct 16, 2017
0
/t5/acrobat-sdk/rounding-calc-error/m-p/9474790#M28266
Using the Community
Experience League
Terms of Use
Privacy Policy
Cookie preferences
AdChoices
Language:

- Deutsch
- English
- Español
- Français
- 日本語コミュニティ
- Português

Copyright © 2020 Adobe. All rights reserved.