Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have an integer value which will always be a whole number, called $post_count
I want to divide $post_count by 2. So if it's an even number, it will always produce a whole result. E.g if $post_count = 8 then I want the result of my arithmetic to be 4.
However if it's an odd number, I wish to presented with the rounded-up number. So if $post_count = 7, I would still want the answer to be 4, because the maths is =
7 / 2 = 3.5
3.5 rounded up = 4
I've written the following code but I am wondering if it's possible to reduce this quite lengthy code into something simpler?
$post_count = $the_query->found_posts;
$post_count = $post_count / 2;
$post_count = round($post_count);
You can use ceil -
$post_count = ceil( $the_query->found_posts / 2 );
If $the_query->found_posts = 7 then it will print 4. ceil will always return the next bigger integer of the current number.
<?php
$posts = 7;
echo round($posts/2);
// 4
You can do like this:
$post_count = round($the_query->found_posts/2);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
In excel rounddown,
rounddown(6432737, -7) = 6430000
rounddown(3456484, -7) = 3450000
How can I do it in PHP?
Please, I appreciate your ideas and answers.
I tried with floor in PHP but it doesn't work.
You can use the floor function in PHP to round down a number to the nearest multiple of a power of 10. For example:
$rounded = floor(6432737 / 10000000) * 10000000; // $rounded will be
6430000
$rounded = floor(3456484 / 10000000) * 10000000; // $rounded will be
3450000
The floor function rounds down a number to the nearest integer, so you can use it to round down a number to the nearest multiple of any number. In the example above, we are dividing the number by 10,000,000 and then multiplying it by 10,000,000 to get the original number rounded down to the nearest multiple of 10,000,000.
You can also use the round function with a negative value for the second argument to achieve the same result:
$rounded = round(6432737, -7);
// $rounded will be 6430000
$rounded = round(3456484, -7);
// $rounded will be 3450000
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to find a way to turn convert 36.901775282237 to 36.91 in php which function do I use in order to convert to this value. I've tried the round function but it's not giving me the right value no matter what flag I use.
The normal way is to use number_format
<?php
$newvalue=number_format(36.901775282237,2);
echo $newvalue;
?>
But if you wish to do what you want , then use a few lines of codes. (I think no need to further explain, just simple math)
<?php
$value=36.901775282237;
//$value=36.900775282237; for this case the result will be 36.90
///// do the trick
$value=intval($value * 1000)/1000;
$parsex=number_format($value * 100,2, ".", "");
$parsex=ceil($parsex)/100;
//// end
echo $parsex;
?>
Mathematically speaking, 36.901775282237 doesn't round up to 36.91 but rather rounds down to 36.90. You'll need to combine ceil with some custom math to get the rounding you're asking for. If you're looking for mathematically correct rounding, number_format will accomplish that internally:
function ceilWithPercision($number, $decimalPoint) {
$number = $number * pow(10, $decimalPoint);
$number = ceil($number);
$number = $number / pow(10, $decimalPoint);
return (string) $number;
}
$number = 36.901775282237;
var_dump(ceilWithPercision($number, 2)); // "36.91"
var_dump(number_format($number, 2)); // "36.90"
Example here: https://3v4l.org/NMOpH
You should take a look at some of the Php Documentation, and maybe some tutorials online before asking these kinds of questions here.
What I think you are looking for is number_format(36.901775282237, 2)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have float values in an array... Let's say one of my values is:
5.1234
How do I SWAP the integer in the float. So in the example above, I'd like to swap the 5 with 8. Therefore the new number would be:
8.1234
This needs to be a SWAP, not a mathematical addition as in 5.1234 + 3.
I basically need to split the number in two, the integer (5) and the float value following it (.1234), swap the 5 for the 8 and the recombine them to get 8.1234.
What is the fastest and most elegant way to do this in PHP since I'll be using this on a LOT of data?
To clarify WHY math cannot be used: This is because this is an obj file that's looking for an usemtl library title (Mudbox compliant) from which it extracts the UV space. Then it changes the vert U (or V) accordingly. Problem is these faces may come up more than once. This would make the operation cumulative, which it is NOT. All it needs to do is substituted the integer.
<?php
$number = 5.1234;
$array = explode(".", $number);
// $array[0] contains 5
$newNumber = 8;
$array[0] = $newNumber;
$finalString = $array[0] . '.' . $array[1];
$finalFloat = floatval($finalString); // String to float
echo $finalFloat;
?>
Here is how I would do this. This solution is relevant if you are sure the number will always be formated like followed :
[number].[decimals]
Else you will not be able to always replace the number before the dot.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
am using application number generator finction like appilcation number logic:current month.current date count of today.year Ex:09.0801.14
first one am getting fine if start second insert application using my logic it shows 09.08802.14
i found that $getresult it geeting 0801 so am getting 5 digit application number
<?php
$getresult=explode(".",$getresult);
if(!isset($getresult))
{
$ref_num=$getmonth.$getdate.'01'.$getyear;
}
else
{
$getresult=intval($getresult[1])+1;
if($getresult<10)
{
$getresult='0'.$getresult;
}
else
{
$getresult=$getresult;
}
$ref_num=$getmonth.$getdate.$getresult.$getyear;
}
?>
here i need to remove $getresult first two digit number i mean if i get $getresult value 0801 i have change it to 01.
how can i do this
Simple bad but fast fix answer:
echo substr('12345', 0, 4)
Output will be 1234
echo substr('12345', 2, 4);
Output will be 34
Use
$getresult = substr($getresult, 2)
to remove the first two digits.
Try this:
$ref_num=substr($ref_num, 2);
example:
0801 - cuts first 2 letters => 01
try:
$result = substr($string, 2);
api of substr here
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is there any way of looping through an array and return each result as it is calculated, rather than waiting until all values are calculated before they are all displayed?
Say I have an array with numbers from 1 to 10 in it, is it possible to loop through the array and return 1, then 2, then 3, then 4, then 5, etc. instead of returning 12345678910 all in one go? Obviously I have simplified this request, but basically my loop calls a function that has a 1 second timeout, so for the user to wait 10+ seconds for all results to be returned rather than a result being returned each second is far less annoying.
Yes, new PHP 5.5 provides such a thing as generators.
function one_to_ten() {
for ($i = 1; $i <= 10; $i++) {
yield $i; //It is like a return, but can be done several times.
}
}
$generator = one_to_ten(); //The loop is not executed yet. We just created the generator.
/* Some big code here */
foreach ($generator as $number){//Only here the iteration starts.
echo "{$number} <br/>" ;
}