When a float number needs to be truncated to a certain digit after the floating point, it turns out that it is not easy to be done. For example if the truncating has to be done to second digit after the point, the numbers should be
45.8976 => 45.89, 0.0185 => 0.01
( second digit after the point is not rounded according to the third digit after the point ).
Functions like round(), number_format(), sprintf() round the number and print out
45.8976 => 45.90, 0.0185 => 0.02
Ben iki çözüm tanıştım ve onlar yeterince iyi ve hangisinin
kullanılmak üzere daha iyi olup olmadığını merak ediyorum
1. function truncNumber( $number, $prec = 2 )
{
return bccomp( $number, 0, 10 ) == 0 ? $number : round( $number - pow( 0.1, bcadd( $prec, 1 ) ) * 5, $prec );
}
2. function truncNumber($number, $prec = 2 )
{
return sprintf( "%.".$prec."f", floor( $number*pow( 10, $prec ) )/pow( 10, $prec ) );
}