math - PHP perfect round off -




i'm trying round off following figures:

case 1: round( ((4/6) * 100), 2 ) . '%'; = 66.67% round( ((1/6) * 100), 2 ) . '%'; = 16.67% round( ((1/6) * 100), 2 ) . '%'; = 16.67%  total % = 66.67 + 16.67 + 16.67 = 100.01%   case 2: round( ((5/11) * 100), 2 ) . '%'; = 45.45% round( ((3/11) * 100), 2 ) . '%'; = 27.27% round( ((3/11) * 100), 2 ) . '%'; = 27.27%  total % = 45.45 + 27.27 + 27.27 = 99.99% 

can tell me how make perfect 100%

thanks

if prefect 100 100.00, use case:

round( ((4/6) * 100), 3 ); = 66.667 round( ((1/6) * 100), 3 ); = 16.667 round( ((1/6) * 100), 3 ); = 16.667 round(66.667+16.667+16.667, 2); = 100  echo round( ((5/11) * 100), 3 ); = 45.455 echo round( ((3/11) * 100), 3 ); = 27.273 echo round( ((3/11) * 100), 3 ); = 27.273 echo round(45.455+27.273+27.273, 2); = 100 

you can make more prefect increasing rounded precision, e.g. use 15 instead of 3 double values ;).
or

$a1 = (4/6)*100; $b1 = (1/6)*100; $c1 = (1/6)*100; round($a1, 2); round($b1, 2); round($c1, 2); round($a1+$b1+$c1, 2);  $a2 = (5/11)*100; $b2 = (3/11)*100; $c2 = (3/11)*100; round($a2, 2); round($b2, 2); round($c2, 2); round($a2+$b2+$c2, 2); 

[php demo]





wiki

Comments

Popular posts from this blog

python - Read npy file directly from S3 StreamingBody -

python - Minimize function with Scipy minimize -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -