I have read the documentation and have clear idea of round but i didn't find useful information to solve the problem.
The problem is i have float number which is let say 1.09 and i want to display it 2 instead of 1. if we use round function it display 1. Help me solve this problem.
MORE DETAILS...
$TotalPaidRemaining=1090;
$monthly_installments=1000;
$MakingNumberOfMonths=$TotalPaidRemaining/$monthly_installments;
echo round($MakingNumberOfMonths, 0, PHP_ROUND_HALF_UP);// it display 1. i want it display 2..
What i want is if the value after decimal point is greater than 0. For example 0.01. I want to consider it as 1.
Hope i am clear at my question.
Use the ceil() function instead.
$number = ceil(0.1); // $number will be 1
From the documentation :
Returns the next highest integer value by rounding up value if necessary.
You can use the ceil() php function instead of round(). It will round up your values. Docs: http://php.net/manual/en/function.ceil.php
Example:
ceil(1.09); // return 2
You could use ceil($yourNumber), which will round the number to its next higher integer.
Or you could use round($yourNumber + 0.499999999999999).
Or you could use floor($yourNumber + 1), which rounds the number to its previous highest integer.
Related
I have a variable $percentchance that represent the percent chance to succeed with something. What I would like help with is, for it to never display above 100 or below 0.
Currently I have used this min function to never go above 100.
<?php echo min(100, $percentchance); ?>
Another issue is that sometimes I get the value of percent like 26.3456, and I wish for some way to make it round up or round down and display only 2 decimals like 26.35 in that case.
You have to set an upper and lower bound for the first problem. The second problem can be tackled with number_format.
Example:
echo number_format(min(100, max(0, $x)), 2);
To enforce always rounding up to the next second digit (e.g. 1.111 would be rounded to 1.12), you could utilise ceil.
echo number_format(min(100, max(0, ceil($x*100)/100)), 2);
if you need to round up with just two decimal use round()
echo round(26.3456, 2);
I have the following variable in PHP:
$number = 1714.2
As you can see with the last decimal I would like to add a 0 if the last decimal number is lower than 10.
What I would like my variable to look like:
1714.02
as .2 and .02 is not same. so, you must have to take correct value form source
or maybe php-manual number format is helpful to you. http://php.net/number_format
1714.2 is not equal to 1714.02
1714.2 means 1714.20 which means 1714.200
But if you want to get the desire result you want just put this code:
$number = $number - 0.18;
If you have a typo and you tried to say:
1714.2 being shown as 1714.20 then refer to:
Set precision for a float number in PHP and number_format
Is this a bug or some math thing I don't understand?
round(58.900662, 2) => 58.9
Where is the last zero?
EDIT:
In my opinion, the documentation is lacking information.
The following code does as expected
number_format(round(58.900662, 2), 2) => 58.90
round will take care of the precision if that digit is not 0 as the last 0 has no effect in any mathematical operation.
If the number is 1.569 then round will return 1.57
If you want that 0 the you can use -
number_format(58.900662, 2, '.', ',');
number_format()
You told it to round it to the 2nd decimal place which would be 58.90. However, 58.90 can be simplified to 58.9. It is the same number.
If you want to force the display of two decimal places you can use PHP's number_format() function.
I have a question regarding number formating in PHP.
I have a variable called "average", which is simply an average of a few values. To make it clear I rounded the number to 2 decimal places. Now the problem is, that if the average is for example 2.90, it only shows 2.9. Is there any way of displaying 2 decimal places always? I though I could do it by multiplying the number by 100, rounding it to zero d.p. and then divide by 100 again, but that seems a bit overcomplicated if there is an easier way of doing it.
Maybe you can try the number_format(float $number [, int $decimals = 0 ])?
For more information, take a look at http://php.net/manual/en/function.number-format.php
Format the output with printf
printf("%.1f", $num); // prints 1 decimal place
printf("%.2f", $num); // prints 2 decimal places
I have small problem and it might be silly somewhere, but still i have it :)
So the problem is:
By doing this
round(615.36*0.10, 2, PHP_ROUND_HALF_DOWN);
I expect outcome to be 61.53, but it's 61.54.
phpVersion = 5.3.2
Could anyone help me to solve this?
Thanks.
PHP_ROUND_HALF_DOWN will round the half -- i.e. the 0.005 part.
if you have 61.535, using PHP_ROUND_HALF_DOWN will get you 61.53 -- instead of the 61.54 you should have obtained with usual rounding.
Basicall, the .005 half has been rounded down.
But 61.536 is not a half : .006 is more than .005 ; so rounding that value gives 61.54.
In your case, you could multiply the value by 100, use the floor() function, and divide the result by 100 -- I suppose it would give you what you expect :
$value = 61.536;
$value_times_100 = $value * 100;
$value_times_100_floored = floor($value_times_100);
$value_floored = $value_times_100_floored / 100;
var_dump($value_floored);
Gives me :
float(61.53)
If you want to round down, you'll need to use floor(), which doesn't have a means to specify precision, so it has to be worked around, eg. with
function floor_prec($x, $prec) {
return floor($x*pow(10,$prec))/pow(10,$prec);
}
you obviously only wanting it to two decimal places why not just number_format(615.36*0.10, 2)