No announcement yet.

javascript problem

  • Filter
  • Time
  • Show
Clear All
new posts

  • javascript problem

    I'm in the process of creating an Acrobat PDF form. I am trying to calculate a text field to round up, a 5 decimal point number, to the nearest 1/16". Does anyone have any idea how to write the javascript code for this?
    JoDee Mullowney
    Ripon Printers

  • #2
    Re: javascript problem

    Well, javascript is horrible at math, first of all.

    Javascript does math as binary calculations. just try to divide 7/5 and tell me what you get.

    The best I can suggest is create an array or hash of the decimal values of all 1/16th values.

    Split your math number into left and right sides of the decimal point and evaluate the right side where the number falls within the 1/16th segments and then just use the next value up.

    switch statements can be very helpful in this case, rather then nesting a whole bunch of if statements.

    Good luck.


    • #3
      Re: javascript problem

      Thank you for the advice. However, my larger problem now, seems to be my lack of javascript know how. I can get throught the basics, but struggle with anything more advanced.

      I've been searching the internet, and the only thing that I have found, at this point, is the conversion of decimal to feet and inches. Not exactly what I was hoping for. I was thinking that I could somehow use some of this code, but I do not understand all of the information within it.
      JoDee Mullowney
      Ripon Printers


      • #4
        Re: javascript problem


        Without doing the work for you...

        If you think about it for a sec. There are only 16 numbers you need to deal with

        0/16 = 0
        1/16 = .0625
        2/16 = .125
        3/16 = .1875
        4/16 = .25
        5/16 = .3125
        6/16 = .375
        7/16 = .4375
        8/16 = .5
        9/16 = .5625
        10/16 = .625
        11/16 = .6875
        12/16 = .75
        13/16 = .8125
        14/16 = .875
        15/16 = .9375
        16/16 = 1

        So if you have a number 0.34567

        split the number into left and right side values (0 and .34567)

        you just have to find where it falls and then use the larger value. since .34567 is greater than 5/16 and less than 6/16 you would use 6/16 or return 0.375

        if you have 2.98765

        (split the number into left and right side values (2 and .98765)

        .98765 would be greater than 15/16 and less than 1 so you would return 3

        Just translate that into a simple javascript function and you should be able to round up anything to the nearest 1/16th of an inch.

        to get you started:
        function double roundUp(double val)
        //Split val
        var tempVal= val + "";  // cast to a string :)
        var tempValArry= tempVal.split("."); // split into array by the .
        var leftside = parseFloat(tempValArry[0]); // leftside value
        var rightside= parseFloat(tempValArry[1]); // rightside value
        // nested shorthand if statement could use if then else as well.
        var rtnVal =
        (rightside<0)    ? 0 :
        (rightside<0.0625)    ? .0625 :
        if (rtnVal == 1)
        rtnVal = leftside + rtnVal;
        return rtnVal;
        Hope that helps... No guarantees the above code would even work... Just a thought starter. I am sure there are much better ways to do what you want to do.


        • #5
          Re: javascript problem

          Thank you VERY much.

          I was able to figure out how to round up to the nearest 1/16" before your post. Now I'm just waiting for the "powers that be" to make a decision as to whether to round up or off to the nearest 1/16". If I need to round off to the nearest 1/16" I can use the information that you have supplied as a baseline.

          Thanks again,
          JoDee Mullowney
          Ripon Printers