Monkuar Posted January 23, 2015 Share Posted January 23, 2015 (edited) 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 January 23, 2015 by Monkuar Quote Link to comment https://forums.phpfreaks.com/topic/294186-kilobyte-numbering-format-system/ Share on other sites More sharing options...
CroNiX Posted January 23, 2015 Share Posted January 23, 2015 (edited) Sure, take a look at the docs for the round() function. Specifically the 2nd parameter. Edited January 23, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/294186-kilobyte-numbering-format-system/#findComment-1503984 Share on other sites More sharing options...
CroNiX Posted January 23, 2015 Share Posted January 23, 2015 It looks like you just need to add: $x_number_format = number_format($x, $decimals); //add $decimals Quote Link to comment https://forums.phpfreaks.com/topic/294186-kilobyte-numbering-format-system/#findComment-1504008 Share on other sites More sharing options...
requinix Posted January 23, 2015 Share Posted January 23, 2015 -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. Quote Link to comment https://forums.phpfreaks.com/topic/294186-kilobyte-numbering-format-system/#findComment-1504009 Share on other sites More sharing options...
CroNiX Posted January 23, 2015 Share Posted January 23, 2015 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]); } Quote Link to comment https://forums.phpfreaks.com/topic/294186-kilobyte-numbering-format-system/#findComment-1504010 Share on other sites More sharing options...
requinix Posted January 23, 2015 Share Posted January 23, 2015 (edited) 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]; } Edited January 23, 2015 by requinix 1 Quote Link to comment https://forums.phpfreaks.com/topic/294186-kilobyte-numbering-format-system/#findComment-1504029 Share on other sites More sharing options...
CroNiX Posted January 23, 2015 Share Posted January 23, 2015 /cronix smacks requinix with a large trout lol, n1 1 Quote Link to comment https://forums.phpfreaks.com/topic/294186-kilobyte-numbering-format-system/#findComment-1504032 Share on other sites More sharing options...
scootstah Posted January 24, 2015 Share Posted January 24, 2015 And they always say bigger is better.... hmm.... Quote Link to comment https://forums.phpfreaks.com/topic/294186-kilobyte-numbering-format-system/#findComment-1504038 Share on other sites More sharing options...
Monkuar Posted January 24, 2015 Author Share Posted January 24, 2015 (edited) 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. But, I've never seen this type of usage before: ["B", "KB", "MB", "GB", "TB"][$log]This part: 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 January 24, 2015 by Monkuar Quote Link to comment https://forums.phpfreaks.com/topic/294186-kilobyte-numbering-format-system/#findComment-1504039 Share on other sites More sharing options...
scootstah Posted January 24, 2015 Share Posted January 24, 2015 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/294186-kilobyte-numbering-format-system/#findComment-1504040 Share on other sites More sharing options...
requinix Posted January 24, 2015 Share Posted January 24, 2015 (edited) Ah, forgot you can't log(0). return round($bytes / pow(1024, ($log = min(floor((float)$bytes == 0 ? 0 : log(abs($bytes)) / log(1024)), 4))), $precision) . ["B", "KB", "MB", "GB", "TB"][$log]; Edited January 24, 2015 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/294186-kilobyte-numbering-format-system/#findComment-1504041 Share on other sites More sharing options...
Monkuar Posted January 24, 2015 Author Share Posted January 24, 2015 (edited) 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 January 24, 2015 by Monkuar Quote Link to comment https://forums.phpfreaks.com/topic/294186-kilobyte-numbering-format-system/#findComment-1504042 Share on other sites More sharing options...
CroNiX Posted January 24, 2015 Share Posted January 24, 2015 (edited) 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 January 24, 2015 by CroNiX 1 Quote Link to comment https://forums.phpfreaks.com/topic/294186-kilobyte-numbering-format-system/#findComment-1504043 Share on other sites More sharing options...
requinix Posted January 24, 2015 Share Posted January 24, 2015 (edited) 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 Edited January 24, 2015 by requinix 1 Quote Link to comment https://forums.phpfreaks.com/topic/294186-kilobyte-numbering-format-system/#findComment-1504047 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.