Re: Converting bytes
- Posted by jmduro Jul 08, 2016
- 2088 views
doncCcole said...
Could you repost your code in the 3.1 form?
It is not that complicated to convert Eu 4 code. Here it is:
include get.e -- format types constant FORMAT_SI = 1000 constant FORMAT_BN = 1024 -- format suffixes constant SUFFIX = {"bytes","K","M","G","T","P","E","Z","Y"} function format_size( atom size, integer ftype ) integer index sequence suffix index = 1 while size >= ftype do -- ftype is 1000 or 1024 size /= ftype index += 1 end while suffix = SUFFIX[index] if index > 1 then if ( ftype = FORMAT_BN ) then suffix &= "iB" suffix &= "B" end if end if return sprintf( "%.3g %s", {size,suffix} ) end function procedure test( atom size ) sequence str1, str2 str1 = format_size( size, FORMAT_SI ) str2 = format_size( size, FORMAT_BN ) printf( 1, "%10d %10s %10s\n", {size,str1,str2} ) end procedure procedure main() integer ok ------------------ -- | SI units | Binary | test( 0 ) -- | 0 bytes | 0 bytes | test( 10000 ) -- | 10 KB | 9.77 KiB | test( 65536 ) -- | 65.5 KB | 64 KiB | test( 10000000 ) -- | 10 MB | 9.54 MiB | test( 16777216 ) -- | 16.8 MB | 16 MiB | test( 1000000000 ) -- | 1 GB | 954 MiB | test( 1073741824 ) -- | 1.07 GB | 1 GiB | ok = wait_key() end procedure main()
Jean-Marc