PHP round different results [duplicate] - php

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).

Related

Trim decimal places before ceil [duplicate]

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);

Is there any way to regex and delete after a certain point in a decimal number in PHP? [duplicate]

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

Need one digit after decimal without changing any number [duplicate]

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);

Fixing to .2 decimal number [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Default Number of Decimal Places to Output in PHP
basically a bit of a maths problem,
$average_ppm = $total_points_given / $totalvalue;
$average_ppm now equals 2.432608695652174, I don't want to display these numbers, I just need $average_ppm to be 2.43, so to a fixed 2 decimal points. How can I do this??
Thanks for anyones time.
Use sprintf if you want a string output, or round/floor/ceil for a numeric value:
$average_ppm = 2.432608695652174;
echo sprintf("%.2f", $average_ppm); // 2.43
$approx_average_ppm = round($average_ppm, 2);
echo $approx_average_ppm; // 2.43
echo floor($average_ppm, 2); // 2.43 , even if $average_ppm = 2.439
echo ceil($average_ppm, 2); // 2.44
You could either use sprintf, round or floor/ceil depending on how you want the numbers rounded.
Most suited for your need would be round:
$average_ppm = round($total_points_given / $totalvalue,2);
If you want to have ALWAYS 2 numbers after... you can do it with number_format:
number_format(2.43260869565217, 2); // 2.43
When you got a number like: 2.400054846 and you use round you will get 2.4
and if you want it with 2 number behind you can use number_format this will output 2.40
I would use Round:
round($average_ppm, 2);
You could use the bcmath functions if they are available where the third argument is the precision
$average_ppm = bcdiv($total_points_given, $totalvalue, 2);

PHP math question [duplicate]

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%

Categories