A common task in many programs is to convert the number of bytes (for example, from disk capacity or file size) to a more human-readable form. Consider 150000000000 bytes as more readable as "150 GB" or "139.7 GB".
Are there libraries that contain functionality to perform these transformations? In python? In c? In pseudo code? Are there any best practices regarding the “most readable” form, for example, the number of significant characters, precision, etc.?
, :
from math import log byteunits = ('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB') def filesizeformat(value): exponent = int(log(value, 1024)) return "%.1f %s" % (float(value) / pow(1024, exponent), byteunits[exponent])
, , . :
, . -, , , . -, , . , Windows, base-1024, , Windows. , base-1024, RAM. , base-1000, .
, , . , , , - , .
Well, I usually go for this:
<?php $factor = 0; $units = ['B','KiB','MiB','GiB','TiB'] while( $size > 1024 && $factor<count($units-1)) { $factor++; $size /= 1024; // or $size >>= 10; } echo round($size,2).$units[$factor]; ?>
Hope this helps!
Source: https://habr.com/ru/post/1774856/More articles:Authentication OData - authenticationTransparency in Java - javaOData by default and configuring service configuration files in Silverlight application created in MS Visual Studio 2013 - visual-studio-2013PHP preg_match_all fails with long lines - phpAdd text in WPF format - wpfVimscript: output to a new (split) window - vimHow to draw a transparent NSScroller - objective-cHow do interactive console programs work? - shellSort comments by votes? - sqlSQL: how to select unique rows with duplicate elements - sqlAll Articles