Page 1 of 1

base:ConvertBytes()

PostPosted: Wed Apr 15, 2015 11:20 pm
by djrikki
This not-so simple function returns bytes as written notation. It returns both a rounded value and the written notation as two separate parameters.

Code: Select all
Global base
base = {}

; Converts filesize in bytes to written notation eg; 1.1 TB, 10.5 GB, 11 MB, 309 KB, 90 bytes
Function base:ConvertBytes(filesize)
    Local bytename

    If filesize >= 1099511627776 ; greater than a tetrabyte
        filesize = filesize / 1099511627776
        filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
        bytename = "TB"
                   
    ElseIf filesize >= 1073741824 ; greater than a gigabyte
        filesize = filesize / 1073741824
        filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
        bytename = "GB"
                       
    ElseIf filesize >= 1048576 ; greater than a megabyte
        filesize = filesize / 1048576
        filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
        bytename = "MB"
   
    ElseIf filesize >= 1024 ; greater than a kilobyte
        filesize = filesize / 1024
        filesize = MidStr(ToString(filesize),0,FindStr(ToString(filesize),".")+3)
        bytename = "KB"
       
    Else
        bytename = "bytes"

    EndIf
   
    return(filesize, bytename)
EndFunction