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

Rounding in Javascript

Participant ,
Feb 14, 2019 Feb 14, 2019

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

791

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

You can round the value in field Q5.

20 Replies 20
Community Expert ,
Feb 14, 2019 Feb 14, 2019

Copied

You can round the value in field Q5.

Report

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

Copied

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

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

Copied

You can use Math.round(x)

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

Copied

Note that setting the field's Format to 0 decimals does NOT actually rounds its 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
Participant ,
Feb 15, 2019 Feb 15, 2019

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.

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

Copied

How does you calculate the value in field Q5?

Report

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

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="";

}

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

Copied

Use this:

`this.getField("Q5").value= Math.round(theValue * 0.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
Participant ,
Feb 15, 2019 Feb 15, 2019

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.

Report

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

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;

Report

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

Copied

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

Report

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

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;

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

Copied

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

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.

Report

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

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="";

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

Copied

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

Report

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

Copied

YES!!!!!!!!  Thank you, Thank you!  I spent hours and hours on this.  I really appreciate 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
Participant ,
Feb 19, 2019 Feb 19, 2019

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="<=";

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

Copied

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

Copied

LATEST

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