Jump to content

More efficient rounding calculator


Go to solution Solved by Barand,

Recommended Posts

Hi All,

I have a rental system - if you rent for 3 or more days over a week you pay for 2 weeks, 3 or more days over 2 weeks and you pay for 3 and so on.

As i need to be able to see how many weeks someone is renting equipment for i have built a js function but wondered if there was a more effience way of getting the weeks value than the following.

            if ($days == 0){
                $weeks = 0
            }else if ($days < 10) {
                $weeks = 1
            } else if ($days >= 10 && $days < 16) {
                $weeks = 2
            } else if ($days >= 17 && $days < 23) {
                $weeks = 3
            } else if ($days >= 24 && $days < 30) {
                $weeks = 4
            }

Thanks as always

Edited by Adamhumbug
Link to comment
https://forums.phpfreaks.com/topic/317201-more-efficient-rounding-calculator/
Share on other sites

19 minutes ago, Barand said:

The code you posted was using PHP variable names. ($days, $weeks)

intdiv (x, y) == floor(x/y)

Apologies, i am using JQuery.

 $weeks = Math.floor($days/7) + ($days%7 >= 3 ? 1 : 0);

The above is working beautifully.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.