Convert Filesize Bytes to Readable String [AS3]
...
@author djankey
converted from JavaScript ("Convert Filesize Bytes to Readable String in Javascript")
Original JavaScript: http://web.elctech.com/2009/01/06/convert-filesize-bytes-to-readable-string-in-javascript/
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.events.MouseEvent;
/**
* ...
* @author djankey
* converted from JavaScript ("Convert Filesize Bytes to Readable String in Javascript")
* Original JavaScript: http://web.elctech.com/2009/01/06/convert-filesize-bytes-to-readable-string-in-javascript/
*/
public class Main extends Sprite
{
private var inputTxt: TextField;
private var outputTxt: TextField;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// label font type
var labelFormat:TextFormat = new TextFormat();
labelFormat.font = "Arial";
labelFormat.color = 0x333333;
labelFormat.size = 14;
// dynamic/input font type
var format:TextFormat = new TextFormat();
format.font = "Arial";
format.color = 0xffffff;
format.size = 14;
// label input
var inputLabel:TextField = new TextField();
inputLabel.x = 50;
inputLabel.y = 170;
inputLabel.defaultTextFormat = labelFormat;
inputLabel.text = "bytes [*input]";
addChild(inputLabel);
// input
inputTxt = new TextField();
inputTxt.x = 50;
inputTxt.y = 192;
inputTxt.defaultTextFormat = format;
inputTxt.background = true;
inputTxt.backgroundColor = 0x333333;
inputTxt.textColor = 0xffffff;
inputTxt.width = 120;
inputTxt.height = 19;
inputTxt.restrict = "0-9";
inputTxt.text = "1073741824";
inputTxt.type = TextFieldType.INPUT;
addChild(inputTxt);
// output label
var outputLabel:TextField = new TextField();
outputLabel.x = 300;
outputLabel.y = 170;
outputLabel.defaultTextFormat = labelFormat;
outputLabel.text = "readable string";
addChild(outputLabel);
// output
outputTxt = new TextField();
outputTxt.x = 300;
outputTxt.y = 192;
outputTxt.defaultTextFormat = format;
outputTxt.background = true;
outputTxt.backgroundColor = 0x333333;
outputTxt.width = 120;
outputTxt.height = 19;
addChild(outputTxt);
// button
var button:Sprite = new Sprite();
button.graphics.beginFill(0x99cc00);
button.graphics.drawRoundRect(185, 190, 100, 24,12);
button.graphics.endFill();
button.buttonMode = true;
button.mouseChildren = false;
addChild(button);
button.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
// button label
var buttonLabel:TextField = new TextField();
buttonLabel.x = 195;
buttonLabel.y = 192;
buttonLabel.defaultTextFormat = format;
buttonLabel.selectable = false;
buttonLabel.text = "CONVERT!";
button.addChild(buttonLabel);
// fist time run
outputTxt.text = readableBytes(Number(inputTxt.text));
}
private function mouseUp(e:MouseEvent):void
{
outputTxt.text = readableBytes(Number(inputTxt.text));
}
private function readableBytes(bytes:Number):String
{
var s:Array = ['bytes', 'kb', 'MB', 'GB', 'TB', 'PB'];
var exp:Number = Math.floor(Math.log(bytes)/Math.log(1024));
return (bytes / Math.pow(1024, Math.floor(exp))).toFixed(2) + " " + s[exp];
}
}
}