Jump to content

Kilobyte Numbering Format System


Monkuar

Recommended Posts

function forum_number_format($number, $decimals = 0)
{
	global $lang_common;
	  $x = round($number);
  $x_number_format = number_format($x);
  $x_array = explode(',', $x_number_format);
  $x_parts = array('k', 'm', 'b', 't');
  $x_count_parts = count($x_array) - 1;
  $x_display = $x;
  $x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
  $x_display .= $x_parts[$x_count_parts - 1];
  return $x_display;
  //return is_numeric($number) ? number_format($number, $decimals, $lang_common['lang_decimal_point'], $lang_common['lang_thousands_sep']) : $number;
}
Let's take a look at this code. This is VERY, VERY hard for me to understand. But it's essentially converting numbers to their kilobyte equivalent. (For example:

 

1000 = 1k.

 

25000 = 25k

 

15000 = 15k

 

My issue is..

 

What happens when the number is 1321

 

This will output 1.3k

 

Why? Because it's using the round function on line 4.

 

But, I'm trying to get it to display 1.32k Any idea how to do this?

 

I've been trying:

 $x = round($number, 1);
and other methods but with no luck. Edited by Monkuar
Link to comment
Share on other sites

-10 points from Slytherin for that awful code.

function forum_number_format($number, $decimals = 2) {
	if ($number > 1e12) {
		return round($number / 1e12, $decimals) . 't';
	} else if ($number > 1e9) {
		return round($number / 1e9, $decimals) . 'b';
	} else if ($number > 1e6) {
		return round($number / 1e6, $decimals) . 'm';
	} else if ($number > 1e3) {
		return round($number / 1e3, $decimals) . 'k';
	} else {
		return round($number, $decimals);
	}
}
Not as "magical" but a helluva lot easier to understand and modify.
Link to comment
Share on other sites

Well if you're going for shorter code :)

function format_bytes($bytes, $precision = 2)
{
  $labels = array('B','KB','MB','GB','TB');
  for($x = 0; $bytes >= 1024 && $x < (count($labels) - 1); $bytes /= 1024, $x++);
  return(round($bytes, $precision).' '.$labels[$x]);
}
Link to comment
Share on other sites

Short, you say?

function format_bytes($bytes, $precision = 2) {
	return round($bytes / pow(1024, ($log = min(floor(log(abs($bytes)) / log(1024)), 4))), $precision) . ["B", "KB", "MB", "GB", "TB"][$log];
}

 

This code is too beast, it even breaks PHP (and my site) and still works. :)

 

af5fcd960d145b2ae545a123f1688e6f.png

 

But, I've never seen this type of usage before:

["B", "KB", "MB", "GB", "TB"][$log]
This part: d4da6f2c2d1be78f289a75a2d65d9115.png

I'm assuming is a shorthand property to merge those 2 arrays correct?

 

Err.. Not 2 arrays, because $log isn't, the ][ is used to merge a string into an array then, correct?

Edited by Monkuar
Link to comment
Share on other sites

No, it is short hand for this:

$labels = array('B','KB','MB','GB','TB');

// $x will be one of: 0, 1, 2, 3, 4

$labels[$x]
It's actually pretty clever. :)

 

How is that possible though, when there is a closing bracket?

 

Edit: Oh, because he didn't assign the B, KB, MB, GB, TB to an array variable, he used an inline array? I think I'm getting it lol.

 

So brackets like that are always shorthand for $array[$value]

 

And $array can be an inline array like what req used. ["hello", "cupcake"][$value]

 

 

Correct?

Edited by Monkuar
Link to comment
Share on other sites

array(1,2,3,4) and [1,2,3,4] are the same thing, if you are using PHP >= 5.4, which added the [] shorthand syntax for array().

 

Just like you can do

foreach([1,2,3,4] as $number)

and

$numbers = [1,2,3,4];
foreach($numbers as $number)

and

foreach(array(1,2,3,4) as $number)

and

$numbers = array(1,2,3,4);
foreach($numbers as $number)

They're all equivalent 

Edited by CroNiX
  • Like 1
Link to comment
Share on other sites

The immediate derefencing part - getting an array value from an array literal (with either syntax) - requires PHP 5.6 though. In PHP

current(array_slice(["B", "KB", "MB", "GB", "TB"], $log, 1))
but easier would be using a string.

trim(substr(" BKBMBGBTB", 2 * $log, 2))
...but then, if I'm doing that,

/* 5.6+ */ ("0KMGT"[$log] ?: "") . "B" // same shorthand deal with strings too
/* <5.6 */ (substr("0KMGT", $log, 1) ?: "") . "B"
That makes use of the fact that "0" == false.

 

?: is short for $a ? $a : $b (since 5.3) so if there was a gun to my head and I had to write for PHP

($log ? substr(" KMGT", $log, 1) : "B") . "B"
Or, you know, one of a billion other ways of making this unreadable.

 

Hopefully obvious disclaimer: don't actually do any of this stuff in real code :tease-01:

Edited by requinix
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.