How do i get the next multiple of 5 in php?
View Discussion Show Improve Article Save Article View Discussion Improve Article Save Article Given a positive integer n and the task is to round the number to the next whole number having divisible by 5. Examples: Input : 46 Output : 50 Input : 21 Output : 25 Input : 30 Output : 30 Approach 1:
Output: 35 Approach 2:
Output: 35 All we need is an easy explanation of the problem, so here it is.I want a php function which returns 55 when calling it with 52. I’ve tried the
It returns 90 but I want 95. Thanks. How to solve :I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you. Method 1This can be accomplished in a number of ways, depending on your preferred rounding convention: 1. Round to the next multiple of 5, exclude the current numberBehaviour: 50 outputs 55, 52 outputs 55
2. Round to the nearest multiple of 5, include the current numberBehaviour: 50 outputs 50, 52 outputs 55, 50.25 outputs 50
3. Round up to an integer, then to the nearest multiple of 5Behaviour: 50 outputs 50, 52 outputs 55, 50.25 outputs 55
Method 2
The value 5 (the resolution / granularity) can be anything — replaced it in both step 1 and 3 So in summary:
Method 3Round down:
Round up:
Round to closest (up or down):
Method 4
I know it’s an old question, but IMHO using modulus operator is the best way, and far more elegant than the accepted answer. Method 5Try this little function I wrote.
Method 6From Gears library
Source:
Method 7Multiply by 2, round to -1, divide by 2. Method 8Here is my version of Musthafa’s function. This one is more complex but it has support for Float numbers as well as Integers. The number to be rounded can also be in a string.
I tried the functions from Knight, Musthafa and even the suggestion from Praesagus. They don’t have support for Float numbers and the solutions from Musthafa’s & Praesagus do not work correctly in some numbers. Try the following test numbers and do the comparison yourself:
Method 9I do it like this:
Tests:
Method 10
Method 11I just wrote this function in 20 min, based on many results I found here and there, I don’t know why it works or how it works!! 😀 I was mainly interested in converting currency numbers from this 151431.1 LBP to 150000.0 LBP. (151431.1 LBP == ~100 USD) which works perfectly so far, however I tried to make it somehow compatible with other currencies and numbers, but not sure if it works fine!!
Feel free to modify it and fix it if there’s anything wrong Method 12Probably you can also consider this one liner. It’s faster! Works for
Note: Use and implement method 1 because this method fully tested our system. All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0 How do you find the next multiple of 5?If it is divisible by 5, return the same number. Else divide it by 5, take floor value and again multiply it by 5 and add 5 as well.. Take the number in a variable.. Divide it by 5 and get the decimal value.. Take the ceil value of the decimal value by using math. ceil().. Multiply it by 5 to get the result.. How do you find the next multiple?A multiple is the result of multiplying a number by an integer. In our case, the number we're multiplying by is six. The integers we've been multiplying by are 11 and 12. That means that the next integer we'll multiply by is 13.
How do I round up in PHP?The ceil() function rounds a number UP to the nearest integer, if necessary. Tip: To round a number DOWN to the nearest integer, look at the floor() function. Tip: To round a floating-point number, look at the round() function.
How do you round to the nearest multiple of 5 in Javascript?To round a number to the nearest 5, call the Math. round() function, passing it the number divided by 5 and multiply the result by 5 .
|