Code to print file sizes as Kb, Mb, Gb etc

View: New views
3 Messages — Rating Filter:   Alert me  

Code to print file sizes as Kb, Mb, Gb etc

by B. Perkins :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This is admittedly not a difficult problem to solve but I was curious is
anyone has and might share code that given a file size in bytes would
return a string showing the size in appropriate Kb, Mb or Gb designation?

Thanks

Brad Perkins

_______________________________________________
Active4D-dev mailing list
Active4D-dev@...
http://mailman.aparajitaworld.com/mailman/listinfo/active4d-dev
Archives: http://mailman.aparajitaworld.com/archive/active4d-dev/

Re: Code to print file sizes as Kb, Mb, Gb etc

by B. Perkins :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Brad Perkins wrote:
> This is admittedly not a difficult problem to solve but I was curious
> is anyone has and might share code that given a file size in bytes
> would return a string showing the size in appropriate Kb, Mb or Gb
> designation?
>
> Thanks
>
> Brad Perkins
I found some PHP code and adapted it.

    method "print_file_size" ($inBytes;$round = 2)
        c_text($outSizeTxt)
        $outSizeTxt := ""
        If (( $inBytes > 0 ) & ( $inBytes < (1024^4) ))
            array text ($sizes;0)
            set array($sizes; " B"; " kB"; " MB"; " GB") `could be
extended for TB, PB, EB, ...
            for ($i; 1 ; size of array($sizes))
                if($inBytes < (1024^$i))
                    $outSizeTxt :=
string(round($inbytes/(1024^($i-1));$round)) + $sizes{$i}
                    break
                end if
            end for
        end if
        return ($outSizeTxt)
    end method

-- Brad

_______________________________________________
Active4D-dev mailing list
Active4D-dev@...
http://mailman.aparajitaworld.com/mailman/listinfo/active4d-dev
Archives: http://mailman.aparajitaworld.com/archive/active4d-dev/

Re: Code to print file sizes as Kb, Mb, Gb etc

by aparajita :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>           array text ($sizes;0)
>           set array($sizes; " B"; " kB"; " MB"; " GB")

In the soon-to-be-released v4.5, this becomes one statement:

array text($sizes; *; " B"; " kB"; " MB"; " GB")

At last, the holy grail of single-line array declaration+definition!

Regards,

    Aparajita
    www.aparajitaworld.com

    "If you dare to fail, you are bound to succeed."
    - Sri Chinmoy   |   www.srichinmoy.org


_______________________________________________
Active4D-dev mailing list
Active4D-dev@...
http://mailman.aparajitaworld.com/mailman/listinfo/active4d-dev
Archives: http://mailman.aparajitaworld.com/archive/active4d-dev/
LightInTheBox - Buy quality products at wholesale price