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);
Related
This question already has answers here:
PHP How do I round down to two decimal places? [duplicate]
(16 answers)
Closed 4 years ago.
Is there any way to do a regex that cuts off a number at a certain point without rounding (simply drops the digits?) say after 4 digits.... It will not be handling negative numbers, EVER. I could have number inputs such as 0.03123 or 1.31, or 10000.98, etc .... What I have written so far as my solution is rounding and not what I'm seeking....
$number = 10000.51999999;
$precision = 4;
echo "<br>";
// grab number before decimal by rounding down the whole number down...
$numberBeforeDecimal = floor($number);
echo "<br>";
// grab the decimal and set the correct precision needed
$n = $number;
intval($n); // 12
$theDecimalPart = explode('.', number_format($n, ($precision)))[1]; // 3430
echo $theDecimalPart; // this is outputting 5200
$theNewValue = $numberBeforeDecimal.".".$theDecimalPart;
explode() the number to get integer and decimal part separated out in an array
Use substr() function to get relevant precision from the decimal part.
Finally, concatenate them back.
Try the following (Rextester DEMO):
$number = 10000.51999999;
$precision = 4;
// separate out the integer and decimal part
$number_str_arr = explode('.', $number);
// concatenate them back
$theNewValue = $number_str_arr[0] . '.' . substr($number_str_arr[1], 0, $precision);
echo $theNewValue; // displays 10000.5199
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:
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).
This question already has answers here:
Closed 10 years ago.
For invoicing I want to determine if my total amount has an unnecessary cent. e.g. $5.01 or $5.51. If it detects the cent then I run an if statement to remove the cent: minus 0.01
eg.
Change 30.51 to 0.01 and give a warning message
and also
30.51 to 0.51
and also
30.51 to 1
Both come from a POST, so they are both PHP strings.
but no worries - i got it to work now thanks to dmayo
$pieces = explode(".", $TotalAmt);
echo "<br>p1:".$pieces[1]; // piece2
$p1 = $pieces[1];
$spl1 = str_split($p1);
$TAmttmp = 0;
//echo "<br>sp11:".$spl1; // error convert array to string
$TAmttmp = $spl1[0];
echo "<br>TAmttmp: with useless cent".$TAmttmp;
echo "<br>IT:".$IT;
if ($TAmttmp == 0.01) //so if there is 1 cent subtract the useless cent.
$TAmt = $TAmt - 0.01;
echo "<br>TAmt: with useless cent".$TAmt;
echo "<br>TAmt: without useless cent".$TAmt;
#Gumbo is right, we need to know the logic behind your conversions to provide a precise answer.
You could do an explode:
$var = explode('.',$_POST['val']);
$digits = $var[1]; // this will give you the digits after the decimal point
Now you can do what you need based on your logic.
You could also you use a substr() where you find the decimal point (is it always three places from the right?) and then grab the substring.
Not sure which would be more efficient.
I don't know exactly what you want, but perhaps that helps:
$nr = "30.51";
echo round($nr - floor($nr), 2); // 0.51
echo round(($nr * 10 - floor($nr * 10)) / 10, 2); // 0.01
echo round(($nr * 10 - floor($nr * 10)) * 10); // 1
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%