This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
Lets say I divide 14 / 15 times it by 100 to get the percentage which is 93.33333333333333 how can I display it as 93.3% using php?
Here is the code.
$percent = ($avg / 15) * 100;
The sprintf function is made for this (see also the manual):
echo sprintf('%.1f%%', $percent);
PHP has a number_format function which lets you specify the number of decimals, what to use for the decimal separator, and what to use for the thousands separator:
$percent = ($avg / 15) * 100;
echo number_format($percent, 1) . '%'; // => 93.3%
Related
This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 2 years ago.
I have a problem with a ceil of a number.
The number is calculated like this. (The actual values comes from other places, this is just to show what is happening. The values in the example are correct to what's happening in the application)
$price = 400;
$multiPlierA = 1;
$multiPlierB = 1.1;
ceil($price * $multiPlierA * $multiPlierB);
which should give me 440. But since $price * $multiPlierA * $multiPlierB ends up being 440.00000000000006 it will of course ceil it to 441. That little floating 6 at the end comes from lovely complementary php magic.
Is there a simple way to get get php to just do this calculation using the first two decimals? I want to trim this off before ceiling 440.00[000000000006....]
You can use number format before ceiling. https://www.php.net/manual/en/function.number-format.php
$price = 400;
$multiPlierA = 1;
$multiPlierB = 1.1;
$result = number_format($price * $multiPlierA * $multiPlierB, 2, '.', '');
ceil($result);
This question already has answers here:
How to round up a number to nearest 10?
(16 answers)
Closed 3 years ago.
Anyone knows how i can convert How to convert numbers like 902.1 or 902 to 900 in php..i have tried both ceil and floor like below but still same issue
$amount = 902.1
$amount2 = floor($amount);
return $amount2;
returns 902
but i want to return either 900 or 910..something like this
This did it for me
$number = ceil($amount / 10) * 10;
This question already has answers here:
Is floating point math broken?
(31 answers)
PHP - Floating Number Precision [duplicate]
(8 answers)
Closed 4 years ago.
In one of my wordpress theme function, this code is being used to calculate total billable amount: ceil(($cost * (1 + ($charges / 100))) * 100);
There is a little miscalculation happening for below scenario.
Scenario:
$charges = 9;
$cost = 100;
echo ceil(($cost * (1 + ($charges / 100))) * 100);
The above code outputs 10901 whereas it should be 10900.
It works fine for other scenarios like:
$charges = 4;
$cost = 90.7;
echo ceil(($cost * (1 + ($charges / 100))) * 100);
//outputs 9433, which is fine because manual calculation results 9432.8
Question:
Why is that happening?
How can I prevent that?
Any alternate function to round to next nearest integer? (only if amount is floating value)
The problem is that you are applying ceil to the outer expression. Try to rewrite it as:
$charges = 9;
$cost = 100;
echo ($cost + ceil($cost * $charges / 100)) * 100;
This outputs 10900 as expected.
UPDATE
As #cars10m suggested, simplifying the expression does help:
echo ceil($cost * 100 + $cost * $charges);
UPDATE 2
You can also use BCMath library to do precise math:
bcscale(6);
echo ceil(bcadd(bcmul($cost, 100), bcmul($cost, $charges)));
This question already has answers here:
Delete digits after two decimal points, without rounding the value
(15 answers)
Closed 8 years ago.
I have a value 3.9609053497942. I need value 3.9 means only one value after decimal. I have used PHP number_format and round functions but it is giving me answer 4.
You could multiply the number by 10, floor() it, and then divide it back.
echo floor($value * 10) / 10;
Try with this,
echo intval((3.9609053497942*10))/10;
or
echo floor((3.9609053497942*10))/10;
There is so many possible solutions:
echo bcadd(3.9609053497942, 0, 1);
preg_match('/\d*\.\d/', 3.9609053497942, $matches);
echo $matches[0];
why not treat it as a string, like
$x = (string)3.96;
$y = explode(".",$x);
$result = $y[0] . "." . $y[1];
Did you tried like:
number_format(3.9609053497942, 1);
This question already has answers here:
PHP Rounding Numbers
(6 answers)
Closed 8 years ago.
Hi i got some problem with rounding. For ex.:
$x = 100;
$y = 4.2030;
$result = round($x / $y, 2);
$result will be 23.79
but now
$result2 = round(23.79 * 4.2030, 2);
$result2 will be 99.99 , so it's incorrect. should be 100 ($result2 equal $X)
how to slove it ?
Your round precision is two decimal places. If you are trying to get whole numbers you need to omit the precision argument:
$result2 = round(23.79 * 4.2030);
NOTE: the lower the precision argument, the more inaccurate your result will be from the actual results.
You can also use ceil() and floor() if you are looking to round in a specific direction (ceil() will round up, floor() will round down).