Search This Blog

Friday, August 2, 2019

Format bytes abbreviation

Whenever dealing with bytes you might want to output an abbreviation for human reading like instead of saying 123456789 bytes saying it's around 118MB. You can do this in OpenAF using the ow.format.toBytesAbbreviation function like this:
ow.loadFormat();

var folderPath = ".";
var sumBytes = ow.format.toBytesAbbreviation( 
                 $from( io.listFiles(folderPath).files).sum("size") )
               );

print(sumBytes);

In this example it will sum all the byte sizes of a given folder and print you the corresponding abbreviation in bytes.

Other examples:
> ow.format.toBytesAbbreviation(10)
10 bytes
> ow.format.toBytesAbbreviation(10000)
9.77 KB
> ow.format.toBytesAbbreviation(10000000000000)
9.09 TB

No comments:

Post a Comment

Using arrays with parallel

OpenAF is a mix of Javascript and Java, but "pure" javascript isn't "thread-safe" in the Java world. Nevertheless be...