ActionScript 3 class to convert file size (in bytes) to human readable format
Examples:
FileSizeUtil.GetHumanFileSize(252163); //returns 264,56 KB
FileSizeUtil.GetHumanFileSize(4455290); //returns 4,24 MB
//////////////////////////////////////////////////
package org.xgeek.markokecman.business.util { import mx.formatters.NumberFormatter; public class FileSizeUtil { /** * Function that converts file size (in bytes) to human readable format * * */ public static function GetHumanFileSize(value:Number):String { var s:Array = new Array('B', 'KB', 'MB', 'GB', 'TB'); if(value < 0){ return "0 " + s[0]; } var con:Number = 1024; var e:Number = logBase(value,con); var numberFormatter:NumberFormatter = new NumberFormatter(); numberFormatter.precision = 2; numberFormatter.decimalSeparatorTo = ","; numberFormatter.thousandsSeparatorTo = "."; var humanSize:String = numberFormatter.format(value/Math.pow(con,Math.floor(e))); return humanSize + ' ' + s[Math.floor(e)]; } /** * Logarithm function with base parameter support * */ private static function logBase (num:Number, base:Number):Number { if (base < 1) base = Math.E; return Math.log (num) / Math.log (base); } } }
Tags: ActionScript, byteconvert, dynamic, filesize