Search code examples
actionscript-3flashdecimalfractionsrational-number

Convert a decimal number to a fraction AS3


I'm trying to get to convert the decimals to a fraction so for example, I had written something such as var _pow:int = Math.pow(base,i) and if i turned out to be a negative number it would give out a decimal (example: 3 ^ -2) and I'm currently stuck trying to find a way to turn _pow into a franction (so _pow out of a 100) so I tried to do var _pow:int = Math.pow(base,i) * 100 which should stop it from being a decimal but it's not showing in the dynamic text, and this only happens if i is negative

package{
    import flash.display.*;
    import flash.events.MouseEvent;

    public class name_ extends MovieClip{

        public function _name_(){
            btn.addEventListener(MouseEvent.CLICK, input)

            base.restrict = "0-9\\.\\-";
            pow.restrict = "0-9\\.\\-";
            answer.multiline = true; 

        }

        private function input(event:MouseEvent):void{
            var pow  = pow.text;
            var base = base.text;
            var answerText:String = "";

            if(pow > 0){
                for(var i = 1; i <= pow; i++){
                     _pow = Math.pow(base,i);
                    answerText += ("\n" + base + " exposant(power) " + i + " = "+ _pow ); 
                    answer.text = answerText; 
                    }

            }else{
                for(i = 1; i <= pow; i++){
                    var _pow:int = Math.pow(base,i) * 100           
                    answerText += ("\n" + base + " exposant(power) " + i + " = "+ _pow ); 
                    answer.text = answerText; //Dynamic text
                }
            }
        }
    }
} 

Solution

  • Have you tried using an "if" statement? Something like if(i <= 0){code}.

    You can also try using the Math.floor(number to be rounded down); or Math.ceiling(number to be rounded up)