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

Problem with my calculations

Explorer ,
May 06, 2024 May 06, 2024

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
6.7K
Translate
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
Community Expert ,
May 06, 2024 May 06, 2024

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.

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

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.

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

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);
}
Translate
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
Explorer ,
May 06, 2024 May 06, 2024

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

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

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

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

Dorothy26138473siql_0-1715034883041.png

 

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

Can you share the new file?

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

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

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

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

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

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;

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

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

Just add this to the end of it:

event.value = gsthalf;

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

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!

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

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

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

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

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

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

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

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!

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

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

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

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. 

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

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?

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

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!!

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

Would these values be correct?

Izrezak.PNG

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

Yes!!

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

Test fields and check if they all works correctly.

https://drive.google.com/file/d/16KUltG4YUcrto3hDyY4CwHD93I8Dl1To/view?usp=sharing 

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

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

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 

 

it was this 

 

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?

Translate
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