Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Lightbulb

    See if you can implement this function into your code...
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title> Untitled </title>
    </head>
    
    <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.
    Last edited by jmrker; 12-24-2013 at 12:15 AM.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    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?
    Evolution - The non-random survival of random variants.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts

    Arrow

    Quote Originally Posted by sunfighter View Post
    ...
    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.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,462
    Thanks
    0
    Thanked 633 Times in 623 Posts
    Quote Originally Posted by sunfighter View Post
    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.


    Quote Originally Posted by bburney View Post
    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)
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    an attempt completely without (floating point) Math: take the total (as string), remove the last character, finis.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

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