Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Rounding issue with Math.floor in a PDF

1. ## Rounding issue with Math.floor in a PDF

I'm creating a form for a free goods promotion where you get one item free for every 10 items purchased.

I am having trouble dividing with the number 10 for some reason.
The code below works great on every number up to 10.
When the "SRHtotal" value is 10 it returns the value of 0 instead of 1.
When the "SRHtotal" value is between 11 and 20 it returns a value of 1.
It does not change to the value of 2 until "SRHtotal" is at 21.

This is my Custom calculation script:

event.value=Math.floor(+getField("SRHtotal").value / 10)

Any help would be appreciated.

• See if you can implement this function into your code...
Code:
```<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<title> Untitled </title>

<body>
<pre id="debug"></pre>

<script type="text/javascript">
function BOG(v) { return Math.floor(i / 10); }

var str = 'Buy up to: ';
for (var i=1; i<100; i++) {
str += i + ' ';
if (i % 10 == 9) {
str += ' Get: '+BOG(i)+ '<br>Buy up to: ';
}
}
document.getElementById('debug').innerHTML = str;
</script>

</body>
</html>```
Alternatively, if you cannot figure it out, then...
Code:
`event.value=Math.floor(+getField("SRHtotal").value / 10)+1;`
if the field is greater than 10.

• Do you think we could see what the function getField() is and the HTML that 'SRHtotal' is a class or id of?
Why do you have the '+' in your equation?

• Originally Posted by sunfighter
...
Why do you have the '+' in your equation?
It turns the supposed string value of the getField into a number for division,
but it is un-needed in the original post as the / would do the same thing.

And, I agree, the original post is lacking somewhat in implementation expectations based upon unknown quantities from the HTML (?) elements.

• Originally Posted by sunfighter
Do you think we could see what the function getField() is and the HTML that 'SRHtotal' is a class or id of?
Why do you have the '+' in your equation?
getField is a built in function that retrieves the value from the PDF form and passes it to JavaScript as a string so there's no more code to see for that than there is for any other built in function.

The + in front converts the string to a number. That works in JavaScript everywhere and not just in PDFs.

There are no such things as class and id in PDFs - you reference PDF form fields by their names. Also there's no HTML in a PDF.

Originally Posted by bburney
This is my Custom calculation script:

event.value=Math.floor(+getField("SRHtotal").value / 10)

Any help would be appreciated.
What it looks like is happening is that the value in the form is not exactly 10 and if it is under by even the smallest amount then it would be rounding down.

You could try the following:

`event.value=Math.floor((+(getField("SRHtotal").value) + .005) / 10)`

• an attempt completely without (floating point) Math: take the total (as string), remove the last character, finis.

• I tried all of the suggestions, unfortunately none worked.
As a last ditch attempt I deleted the fields and recreated them with no formatting and just code....that worked.
I'm not sure what caused the problem but I will play with it more and see if I can find the setting that was to blame.

Thank you all VERY MUCH for the help.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•