package com.lookmum.util { public class Fraction { private static var it :Number = 0; public static var iterationLimit:Number = 10000; public static var accuracy :Number = 0.00001; public function Fraction() { } private static function resetIt():void { it = 0; } private static function addIt():Boolean { it++; if (it == iterationLimit) { trace('error : too many iterations'); return true; } else { return false; } } public function getFractionString(num:Number):String { var fracString:String; var fracArray:Array = getFraction(num); switch (fracArray.length) { case 1 : fracString = num.toString(); break; case 2 : fracString = fracArray[0].toString() + '/' + fracArray[1].toString(); break; case 3 : fracString = fracArray[0].toString() + ' ' + fracArray[1].toString() + '/' + fracArray[2].toString(); break; } return fracString; } public function getFraction(num:Number):Array { var fracArray:Array = new Array(); var hasWhole:Boolean = false; if (num >= 1) { hasWhole = true; fracArray.push(Math.floor(num)); } if (num - Math.floor(num) == 0) { return fracArray; } if (hasWhole) { num = num - Math.floor(num); } var a:Number = num - int(num); var p:Number = 0; var q:Number = a; resetIt(); while (Math.abs(q - Math.round(q)) > accuracy) { addIt(); p++; q = p / a; } fracArray.push(Math.round(q * num)); fracArray.push(Math.round(q)); return fracArray; } } }
Personal blog were I will post as3 snippets that will help all AS3 programmers to achieve small tasks, not always simple or straightforward.
Showing posts with label as3 fraction numbers. Show all posts
Showing posts with label as3 fraction numbers. Show all posts
Thursday, 5 November 2009
Decimal to Fraction
This class will be very handy to transform a decimal number to a fraccion. This was taken from forum entry in Actionscript.org and the user "rondog" converted a AS2 class to AS3. This is one more place where you can get it.
Etiquetas:
as3 fraction numbers
Subscribe to:
Posts (Atom)