Hi
Trying to do some simple division with KB
to get something like 0.5 megabyte
best round gives me is 0.49PHP Code:
$sum = round(500.30 / 1024, 2);
echo $sum;
Thanks
Sonny

Hi
Trying to do some simple division with KB
to get something like 0.5 megabyte
best round gives me is 0.49PHP Code:
$sum = round(500.30 / 1024, 2);
echo $sum;
Thanks
Sonny
Which is correct though. The numbers you have are ~0.48857, which rounded to two decimal points is 0.49. If you want 0.5, you round to 1 decimal point, but that means ever calculation will round to the nearest 1/10th instead of 1/100th.
http://php.net/manual/en/function.round.php
set number of decimal places to 1 and you'll get 0.5
FouLu
How do you display lets say 0.7 mb?
I messed with floor and others but could
not get it.
If I set round to 1 decimal, it will just just
round up to one mb. floor will round down
to 0 mb
Sonny
round(0.732131312, 1) will return 0.7
that's how it works. also there are flags you can set as third argument if you want other type of rounding.
check link i've posted above for details
You have to choose between using a printformatter, rounding, or casting. You cannot have the best of both worlds unless you want to start handling on steps of 512 instead of 1024.
Using a printf will truncate off the remaining digits. But that means your 500.30 would result in 0.49. Rounding will make 500.30 into 0.5, but will convert 0.99 to 1.0. You need to choose.
Or of course you can simply write something that shows < 1024KB as a KB rating, and handle it from there. Then you'd see 500.30KB, and at 1033KB you'd see 1.01MB. Easiest done if you measure in a nonmultiple unit (bytes). Writing a function like this would be trivial.