Skip to main content
stepheng54012748
Known Participant
Feb 15, 2019 3:06 AM UTC
Answered

Rounding in Javascript

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

This topic is closed to new replies. Start a new post to keep the conversation going.
Bernd Alheit
Bernd AlheitCorrect answer
Community Expert
Feb 15, 2019 6:25 AM UTC

You can round the value in field Q5.

20 replies

stepheng54012748
Known Participant
Feb 15, 2019 2:59 PM UTC

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

}

Translate
Bernd Alheit
Community Expert
Community Expert
Feb 15, 2019 3:04 PM UTC

Use this:

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

Translate
stepheng54012748
Known Participant
Feb 15, 2019 3:34 PM UTC

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. 

Translate
Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
Feb 15, 2019 6:25 AM UTC

You can round the value in field Q5.

Translate
stepheng54012748
Known Participant
Feb 15, 2019 6:28 AM UTC

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

Translate
Bernd Alheit
Community Expert
Community Expert
Feb 15, 2019 8:55 AM UTC

You can use Math.round(x)

Translate