Skip to main content
Participant
April 25, 2024
Answered

Rounding down a calculation

  • April 25, 2024
  • 1 reply
  • 665 views

Hello Group, 

I am working on a converting a PDF to a fillable form. There are mulptiple equations that I have solved so far but I am stuck on this one. The calculation that I am using is figuring out how many trees per acre is needed. I have the field formatted to only show the integer, but in another calculation using the sum below, it adds the decimals to the end changing the final number Since I can not plant a fraction of a tree, I want to drop everything past the decimal. 

Thanks in advance, y'all are awesome! 

 

// Custom calculate script
// Get the field values, as numbers
var numerator = +getField("Spacing1").value;
var denominator = +getField("Spacing2").value;
// Calculate and set this field value
if (denominator !== 0) {event.value = 43560/(numerator * denominator);} else {event.value = "";}

This topic has been closed for replies.
Correct answer Nesa Nurani

To round down, you can use Math.floor(),

also, since you multiply numerator and denominator, you need to check that both are not 0.

Try this:

var numerator = Number(this.getField("Spacing1").valueAsString);
var denominator = Number(this.getField("Spacing2").valueAsString);

if (denominator !== 0 && numerator !== 0) {
event.value = Math.floor(43560/(numerator * denominator));} 
else {
event.value = "";}

 

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
April 26, 2024

To round down, you can use Math.floor(),

also, since you multiply numerator and denominator, you need to check that both are not 0.

Try this:

var numerator = Number(this.getField("Spacing1").valueAsString);
var denominator = Number(this.getField("Spacing2").valueAsString);

if (denominator !== 0 && numerator !== 0) {
event.value = Math.floor(43560/(numerator * denominator));} 
else {
event.value = "";}