• Global community
• Language:
• Deutsch
• English
• EspaÃ±ol
• FranÃ§ais
• PortuguÃªs
• æ—¥æœ¬èªžã‚³ãƒŸãƒ¥ãƒ‹ãƒ†ã‚£
Dedicated community for Japanese speakers
• í•œêµ­ ì»¤ë®¤ë‹ˆí‹°
Dedicated community for Korean speakers
Exit
• Problem with my calculations

# Problem with my calculations

Explorer ,
May 06, 2024 May 06, 2024

Copied

Hello, I am having some problems with the calculations for our GST on my form. The GST 50 column should take the GST from gst.1 divide by half first and then round down second. It only needs to round down if the number is not perfectly divisible by 2 though. so if I enter 16.06 which is an even number, as the GST it should round to 8.03 but its not, its rounding down to 8.02.  If I have say 5.27 that isn't easily divisible, the government pays the lower half, we pay the higher half, so I need that to round down to 2.62.

I had to redo my calculations for the Receipt total, GST on receipt  km \$ and Total meals columns to the sum method,  but it seems to have broken my calculation script and validation script when i did that. I have attached the file.

Any assistance would be amazing!

TOPICS
General troubleshooting , How to , JavaScript , PDF , PDF forms

Views

654

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
31 Replies 31
Community Expert ,
May 06, 2024 May 06, 2024

Copied

After rounding the number to two decimals you can multiple it by 2. If the result doesn't equal the original value (because it's an "odd" number that was rounded) then reduce 0.01 from it.

Report

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

Copied

Thank you, however, I don't know exactly how to do that Lol this script was written for me, I am only just starting to learn javascript.

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 ,
May 06, 2024 May 06, 2024

Copied

Try this:

``````var gst = Number(this.getField("gst.1").valueAsString);
var gsthalf = roundDown(gst / 2, 2);
if ((gsthalf*2)!=gst) {
gsthalf-=0.01;
}

function roundDown(v, nDecimals) {
return Math.floor(v * Math.pow(10, nDecimals)) / Math.pow(10, nDecimals);
}``````

Report

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

Copied

so that worked for the even one, but not the odd, its rounding up instead of down....

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 ,
May 06, 2024 May 06, 2024

Copied

Not for me... I entered 5.27 and it returned 2.62.

Report

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

Copied

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 ,
May 06, 2024 May 06, 2024

Copied

Can you share the new file?

Report

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

Copied

Here you go, I only changed the one in the first line.

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 ,
May 07, 2024 May 07, 2024

Copied

I don't see the code I provided in this file.

Report

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

Copied

GSThalf1 has the new code in the custom validation script. and it has this as the calculation script.

event.value = Number(this.getField("gst.1").valueAsString) / 2;

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 ,
May 07, 2024 May 07, 2024

Copied

Why a validation script? It needs to replace your current calculation script.

Just add this to the end of it:

event.value = gsthalf;

Report

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

Copied

haha because i totally thought it was a validation script, likle i said i am still learning!  This seems to be working ðŸ™‚

Thank you so much for your help and putting up with me!

Report

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

Copied

so, i spoke too soon, when i put a round number in, like says 16.06 its making it 8.01

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 ,
May 07, 2024 May 07, 2024

Copied

Again, you need to share the file for help... Also, set the field's Format setting to None.

Report

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

Copied

My apologies, here is the form. I did have this in the format box, to prevent the 0 from being in the field

if((event.value == 0) || isNaN(event.value) || (event.value == Infinity))
event.value = "";
else
event.value = util.printf("\$%,0.2f",event.value);

Report

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

Copied

Hi not sure if you saw my last message? I do very much appreciate you  helping me out, I go on vacation after tomorrow and my boss is standing at my desk asking me if I know what i am doing LOL which i obviously dont!

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 ,
May 08, 2024 May 08, 2024

Copied

When you enter 5.27 what should be result for you and what result for government?

Report

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

Copied

so if the amount is even, say .06 it should be .03 each. If the amount is odd, .23 or .27 etc , ours needs to be the rounded up, government is rounded down. So .27 is .12 for government and .13 for us, .23 is .11 for governemnt .12 for us.

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 ,
May 08, 2024 May 08, 2024

Copied

That doesn't sound correct, if you have 5.27, you say government pay 2.62, and you pay 2.63?

That equals to 5.25, so where are 2 cents went?

Shouldn't correct result be government 2.63 and you 2.64?

Report

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

Copied

obviously my math skills arent up to snuff this morning! sorry. 5.27 would be 2.63 and 2.64 we pay the 2.64

Sorry!!

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 ,
May 08, 2024 May 08, 2024

Copied

Would these values be correct?

Report

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

Copied

Yes!!

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 ,
May 08, 2024 May 08, 2024

Copied

Test fields and check if they all works correctly.

If you need to know all the changes I have made, let me know, and I will send you PM with description of changes.

Report

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

Copied

So this is good the only thing i see that is a problem now is that the program costs at the end, that needs to add up receipt total, km\$ Total MEals and GST

var gst = Number(this.getField("gst.1").valueAsString);
var rectotal = Number(this.getField("rec.1").valueAsString);
var km = Number(this.getField("km.1").valueAsString);
var meals = Number(this.getField("meal.1").valueAsString);
var v = gst / 2;
var nDecimals = 2;
var gsthalf = Math.ceil(v * Math.pow(10, nDecimals)) / Math.pow(10, nDecimals);
event.value = (rectotal-gst)+km+meals+gsthalf;

can i just put that back in myself?

Report

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