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

Rounding in Javascript

Participant ,
Feb 14, 2019 Feb 14, 2019

Copy link to clipboard

Copied

If anyone can help resolve a real headache in writing a javascript that will round correctly, I would be so very much appreciative!  I am very new at writing Javascript, but I am getting better and more creative all of the time.  Right now, I am absolutely stumped.  I'm designing form, and I've run into an issue with writing a javascript with validation.  Here is the situation:

1.  I have three columns.  The first column is a number which is input by the person filling out the form.  As an example they might enter $75,480 into column one.

Column 2 and column 3 are numbers to be entered by the person filling out the form.

2.  Column 2:  Since column 2 is a number input by the person filling out the form, I wrote a validation script as follows:

var a = this.getField("Q5").value;

if (event.value > a)event.value="";

else event.value;

Q5 is a hidden field that I wrote that is a value based on $75,480 x 0.01 = $754.80.

I wrote the validation so the number enter in column 2 does not exceed 1% of $75,480.  Also, the result in Column 2 has no decimals so the result would be $755.  If someone enters $754.80 into the field the result would be $755.  However, if the person enters $755 into the field the value goes blank because it exceeds the $754.80. 

I can't figure this out with my simple programming skills.  I feel like the answer is to have the validation result be a whole number $755 and not the $754.80.  I'm assuming if the validation were equal to the rounded number then I would get a result in column 2.

3.  Column 3 is the same situation with a different percent multiplied.  I'm thinking if I can resolve Column 2 situation i will be able to write the validation for Column 3.

THANK YOU in advance for any feedback.

Sincerely,

Steve

TOPICS
Acrobat SDK and JavaScript , Windows

Views

828

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 , Feb 14, 2019 Feb 14, 2019

You can round the value in field Q5.

Votes

Translate

Translate
Community Expert ,
Feb 14, 2019 Feb 14, 2019

Copy link to clipboard

Copied

You can round the value in field Q5.

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
Participant ,
Feb 14, 2019 Feb 14, 2019

Copy link to clipboard

Copied

Can you help me with writing the script?  I'm really struggling trying to figure it out.

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
Community Expert ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

You can use Math.round(x)

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
Community Expert ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

Note that setting the field's Format to 0 decimals does NOT actually rounds its value.

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
Participant ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

Thank you for responding.  I know that setting the format to 0 doesn't really round.  Will Math.round(x) round the number or will it leave the value as decimals in the field?  I still don't understand how to write the script that i'm attempting to write with Math.round(x). I'm trying but this one really has me stumped.

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
Community Expert ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

How does you calculate the value in field Q5?

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
Participant ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

var theField = this.getField("TF2A6");

var theValue = theField.value;

if (theValue > 0.01) {

    this.getField("Q5").value= (theValue * 0.01);

}

else {

     this.getField("Q5").value="";  

}

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
Community Expert ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

Use this:

this.getField("Q5").value= Math.round(theValue * 0.01);

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
Participant ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

Yes!  Rounding Beautifully.  Thank you, Thank you!

Last question.  Now that it's rounding, I have one other problem.  When I enter a number in Column 2, if it is the exact rounding number it enters perfectly as the rounded number.  If it is not the rounded number it disappears.  I wrote a validation:

var a = this.getField("Q5").value;

if (event.value > a)event.value="";

else event.value;

I wrote the validation so the number enter in column 2 does not exceed 1% of $75,480. I would be great if the amount entered in Column 2 result in $755 no matter if you entered $754.80 or $755. 

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
Participant ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

I'm just wondering if I should just write a validation formula with Math.Roundup for Column 2?  This way if someone enters $754.80 or $755 then it will accept either but not exceed the rounded number.  Seems like this might be the result I need.  So, below is my attempt to write a validation formula.  What do you think?

var a = this.getField("TF2A6").value;

if (event.value > a)event.value=Math.round(theValue * 0.01);

else event.value;

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
Participant ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

One other comment is the person entering the number can be $755 or less. 

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
Participant ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

Is it possible to write a validation statement to be <=?  Can I alter the below validation to do this?

var a = this.getField("Q5").value;

if (event.value > a)event.value="";

else event.value;

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
Community Expert ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

Yes and yes.

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
Community Expert ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

PS. The last line of your code is meaningless. You should remove it, or change it to apply a new value to event.value.

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
Participant ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

Ok, so, I believe I've removed the meaningless part.  I want the result to be "" if it is higher than the rounded number.  Where in this statement can insert <= the rounded amount??  Would i add to the statement:  if (event.value < a)event.value

var a = this.getField("Q5").value;

if (event.value > a)event.value="";

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
Community Expert ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

Replace ">" with "<="...

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
Participant ,
Feb 15, 2019 Feb 15, 2019

Copy link to clipboard

Copied

YES!!!!!!!!  Thank you, Thank you!  I spent hours and hours on this.  I really appreciate it!

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
Participant ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

Well, i thought i had this.  Seems like i'm having one more issue.  If someone tries to put in a higher number than allowed it doesn't allow for the number however, if you put you cursor in the field it shows <=.  The person needs to clear the <= in order to enter a number that will be evaluated.  Interestingly, everything seems to be working correctly just need to figure out how to not have the <=.  Here is what I wrote as a validation formula.  When I tried removing the <= from inside the parenthesis the field would no longer validate correctly.  Thank you again for helping me get this far with this.

var a = this.getField("Q1").value;

if (event.value > a)event.value="<=";

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
Community Expert ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

Use event.value = ""

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
Participant ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

LATEST

Ok.  That worked.  Not sure what i was doing incorrectly but thank you again!!!

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