base:ConvertBytes()

You can post your code snippets here for others to use and learn from

base:ConvertBytes()

Postby djrikki » Wed Apr 15, 2015 11:20 pm

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
Evolve - Rapid GUI Development tool for MUI Royale and RapaGUI
http://myevolve.wordpress.com
djrikki
 
Posts: 645
Joined: Wed Apr 06, 2011 12:26 am

Return to Code snippets area

Who is online

Users browsing this forum: No registered users and 1 guest