Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Rounding a calculation to the nearest 0 or 5

Rounding a calculation to the nearest 0 or 5

Scheduled Pinned Locked Moved JavaScript
javascripttoolshelplearning
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DiesenDesign
    wrote on last edited by
    #1

    I created a PDF form from an excel file. I have everything figured out, except one of the fields in which I am able to round the answer up to the nearest integer, but I need to take it one step further and round it to the nearest 5 or 0. This is what I an using as my custom calculation script: (Math.ceil(CourseHours/40) + "
    "); I have just started learning javascript so I found this off of a website. If anyone could help I would certainly appreciate it. Thank you in advance.

    A S 2 Replies Last reply
    0
    • D DiesenDesign

      I created a PDF form from an excel file. I have everything figured out, except one of the fields in which I am able to round the answer up to the nearest integer, but I need to take it one step further and round it to the nearest 5 or 0. This is what I an using as my custom calculation script: (Math.ceil(CourseHours/40) + "
      "); I have just started learning javascript so I found this off of a website. If anyone could help I would certainly appreciate it. Thank you in advance.

      A Offline
      A Offline
      Aniruddha Loya
      wrote on last edited by
      #2

      Use

      Math.round( n /5) * 5;

      where n is the number you are looking to round off.

      Imagination is the one weapon in the war against reality!!! aniruddhaloya.blogspot.com

      D 1 Reply Last reply
      0
      • A Aniruddha Loya

        Use

        Math.round( n /5) * 5;

        where n is the number you are looking to round off.

        Imagination is the one weapon in the war against reality!!! aniruddhaloya.blogspot.com

        D Offline
        D Offline
        DiesenDesign
        wrote on last edited by
        #3

        That did not work but looking at my email again, the entire script didn't paste. It is (Math.ceil(CourseHours/40)+"
        "); Sorry about that. Thanks much.

        A 1 Reply Last reply
        0
        • D DiesenDesign

          That did not work but looking at my email again, the entire script didn't paste. It is (Math.ceil(CourseHours/40)+"
          "); Sorry about that. Thanks much.

          A Offline
          A Offline
          Aniruddha Loya
          wrote on last edited by
          #4

          I guess your entire script is not there again... Anyways... Math.ceil will always take it to next higher number whereas Math.round round it to the nearest integer. So you can choose as per your requirement. From what I think you are looking to do is - You require (CourseHours/40) rounded to nearest 0 or 5... i.e. the number you eventually looking to round off is the output of - CourseHours/40... So for my solution to work - 'n' should be substituted by - CourseHours/40 i.e

          Math.ceil(CourseHours/40/5) * 5

          or use Math.round as per your requirement. I hope this helps.

          Imagination is the one weapon in the war against reality!!! aniruddhaloya.blogspot.com

          D 1 Reply Last reply
          0
          • D DiesenDesign

            I created a PDF form from an excel file. I have everything figured out, except one of the fields in which I am able to round the answer up to the nearest integer, but I need to take it one step further and round it to the nearest 5 or 0. This is what I an using as my custom calculation script: (Math.ceil(CourseHours/40) + "
            "); I have just started learning javascript so I found this off of a website. If anyone could help I would certainly appreciate it. Thank you in advance.

            S Offline
            S Offline
            Satheesh1546
            wrote on last edited by
            #5

            Hi, Try Math.floor() Thanks,

            1 Reply Last reply
            0
            • A Aniruddha Loya

              I guess your entire script is not there again... Anyways... Math.ceil will always take it to next higher number whereas Math.round round it to the nearest integer. So you can choose as per your requirement. From what I think you are looking to do is - You require (CourseHours/40) rounded to nearest 0 or 5... i.e. the number you eventually looking to round off is the output of - CourseHours/40... So for my solution to work - 'n' should be substituted by - CourseHours/40 i.e

              Math.ceil(CourseHours/40/5) * 5

              or use Math.round as per your requirement. I hope this helps.

              Imagination is the one weapon in the war against reality!!! aniruddhaloya.blogspot.com

              D Offline
              D Offline
              DiesenDesign
              wrote on last edited by
              #6

              It seems to work, but what I am finding is that, as most of the answers will be less than 1, it is rounding it to the nearest 0 or 5 in the thousandths when I need it to be no larger then hundreths. SO all I should have as the final answer is .15 or .35. Does that make sense?

              A 1 Reply Last reply
              0
              • D DiesenDesign

                It seems to work, but what I am finding is that, as most of the answers will be less than 1, it is rounding it to the nearest 0 or 5 in the thousandths when I need it to be no larger then hundreths. SO all I should have as the final answer is .15 or .35. Does that make sense?

                A Offline
                A Offline
                Aniruddha Loya
                wrote on last edited by
                #7

                You can try using "Number".toFixed(x) along with the existing solution for fixing that. More info of your data set may help to find a better solution though :)

                Imagination is the one weapon in the war against reality!!! http://aniruddhaloya.blogspot.com

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups