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);
Related
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.
I have a code in which calculates the Effective interest rate.
when I echo it I get 5.1161897881733 which I want to limit the remainder and output it like 5.11, is there any functions use to limit the remainder in php?
A billion ways to do this. The task for you here is to pick one.
Method 1 - round()
This will just round your number. The exact rules can be found in the PHP.net documentation.
round($someNumber, 2);
Method 2 - floor()
Floor will round the number down
floor($someNumber, 2);
Method 3 - ceil()
Opposite to floor() this will round your number upwards.
ceil($someNumber, 2);
Method 4 - number_format()
This will format any number. number_format() has a gazillion possible inputs in which you can choose decimal characters etc.
// Will round your number to 2 decimals with a . as decimal character
number_format($someNumber, 2, ",", ".");
Feel free to edit and add more options :)
round($result, 0)
The 0 represents the decimal places.
http://php.net/manual/en/function.round.php
Doing an billing application in php, There come price with decimals like 0.576 tried
round()
number_format()
it will give me 0.58 but i want 0.57 only, how can i get in php?
Something like this?
floor(0.576*100)/100
//0.57
Using round with PHP_ROUND_HALF_DOWN wont work as it only affect the way decimal 5 is rounded. It does not truncate the float value.
round(0.576, 2, PHP_ROUND_HALF_DOWN);
//0.58 //Not good
round(0.575, 2, PHP_ROUND_HALF_DOWN);
//0.57 //Good but will not work above this value (e.g 0.0576, 0.0577...)
A fraction higher than .5 is always rounded up. If you already have found round why haven't you looked at the documentation:
http://www.php.net/manual/en/function.round.php
Try:
echo round( 0.676, 2, PHP_ROUND_HALF_DOWN );
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
Is there a PHP function that will make the number always have 2 decimals places, even if it's 0?
No, but you can format a number to a string with decimals
number_format — Format a number with grouped thousands
Example:
echo number_format(0, 2); // 0.00
EDIT: the printf/sprintf solutions suggested deserve some upvotes too
Do you mean you want to print additional digits even if there's only one digit after the decimal? You can do something like:
$x = 0.1;
printf("%.2f", $x);