Skip to content

glances display: does G mean 10^9 or 2^30 bytes? #2585

Answered by nicolargo
Twixtfanatic asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @Twixtfanatic,

sorry for the respond delay.

The following method is used to convert units to human readable string:

def string_value_to_float(s):
    """Convert a string with a value and an unit to a float.
    Example:
    '12.5 MB' -> 12500000.0
    '32.5 GB' -> 32500000000.0
    Args:
        s (string): Input string with value and unit
    Output:
        float: The value in float
    """
    convert_dict = {
        None: 1,
        'B': 1,
        'KB': 1000,
        'MB': 1000000,
        'GB': 1000000000,
        'TB': 1000000000000,
        'PB': 1000000000000000,
    }
    unpack_string = [
        i[0] if i[1] == '' else i[1].upper() for i in re.findall(r'([\d.]+)|([^\d.]+)'…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Twixtfanatic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants