开发者

PHP Convert numbers to data size

I cant find anything on this specifically.

I need to convert each floored value to represent to GB unit (data). Example:

  1. 5 (convert to 5368709120 - 5GB)
  2. 8 (convert to 8589934592 - 8GB)
  3. 11 (convert to 11811160064- 11GB)

A user redeems coins in a game, that round to a base value (say, 5) and I need the return value (5) to represent 5368709120 -- not 5 --

$coins = 887;
$deduct = floor($coins / 100) * 100;
$retain = max(0, $coins - $deduct);
$value  = round(($deduct / 100) / 2);

^ We see 4. How can we convert it to be valid size for 4GB?

Basically, how can I convert these single numbers to repen开发者_运维问答t actual data sizes? Is there a formula for this?

Thank you


I know this might not be what you asked for, but I believe you might be missing the point, my usual approach for things like this is that i send the actual value like $coins from the server to the client and do all the post processing (as much as possible) there, because:

  1. server-side performance is king. imagine if your conversion (which can sometimes be complicated) takes 50 millisecond of time. if that 50 millisecond happens on the user device on the client side, the user wont even notice the difference, while you server is saving resources (if you optimize all your end-point and you get 100 request per second that's 5 seconds of processing time ;D of course these are all hypothetical just to get my point through)
  2. you always have the original data and can show it in different formats with no data loss (like you example)
  3. the only time I don't send raw data to client is when client accessing that raw data can have security concerns attached to it.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜