I have a little code that does a certain calculation and as you know if decimals are printed PHP
many come out, what I want is to limit this number to 5 decimals.
Try to use round
and although with this you can specify a maximum number of decimal places it also rounds them and no, I only want a number of decimal places without rounding the result.
For example:
The result throws:
4.6472883
And what I need is:
4.64728 (5 decimales)
What it round
does is:
4.64729
This is the small part that does the calculation in case it is useful to you
//$l_value: es un valor que ingresa el usuario
//$l_mam y $l_ang: son valores ya definido
$to_m = $l_value*$l_mam;
$l_r = $to_m/$l_ang;
echo"<div class='in_val_res' > ".round($l_r,5)." unidades</div>";
You can use the native function
bcdiv
.bcdiv divides two arbitrary precision numbers with the addition that allows you to trim the result to a given scale without rounding.
If the division is by
1
then the result is always the number but you use the scale to trim to your liking.Example
Result
Multiply it and divide it by as many decimal places as you want. (1->10 | 2->100 | ...)
Although we use floor (which is for rounding), thanks to multiplication and division you get the desired result.
I don't know if there will be a specific function, I guess there is... but you can "make" a "handmade" truncation. Multiply the number to move the "comma" to the right (for example by 100000) and take the integer part of that result and divide that number again by 100000... I don't know, it occurs to me... That's how it works the idea...
As they comment in another answer, the easiest thing would be to do a truncation, in my case I would do it in the following way, with the function intval() and pow (or double asterisk ** ):