1

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
                }
            }
        }
    }
} 
Jyreeil
  • 75
  • 1
  • 11
Ulric10
  • 13
  • 4
  • A stupid question: your code allows `pow` to be fractal. What should it return if you've got `3.5` for base and `-1.33` for pow? – Vesper Mar 06 '20 at 07:06
  • One way I would do it is to convert a decimal to a fraction based on the number of decimal places used and then determining the greatest common factor of the two numbers. Like 0.2 -> 2/10 -> 1/5... 0.25 -> 25/100 -> 1/4... 0.275 -> 275/1000 -> 11/40. At face value it doesn't seem super difficult, but I don't know how hard it is to find greatest common factors. – ugotopia123 Mar 06 '20 at 07:40

1 Answers1

1

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)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Jyreeil
  • 75
  • 1
  • 11
  • It doesn't seem to work(the if statement), I also tried the part where I try out Math.ceil (it doesn't seem to work either) `for(i = 1; i <= pow; i++){ var _pow:int = Math.ceiling( Math.pow(base,i)) * 100 answerText += ("\n" + base + " exposant(power) " + i + " = "+ _pow ); answer.text = answerText; //Dynamic text ` but it still gives out the same result to, even with the if statement, it doesn't show up in the dynamic text, only when `_pow` is positive it shows – Ulric10 Mar 06 '20 at 03:35
  • Ok so I just reread your code and the declaration of "_pow" is "pow" that is in conflict with the function of Math.pow(base, i). Seems like you have a lot of syntax errors as well, but those should not let the program compile. These two lines should have pow = _pow, and also include a variable type. var pow = pow.text; var base = base.text; This line you don't need to declare the variable cause you already have in that function. var _pow:int = Math.pow(base,i) * 100 You probably don't need the * 100 unless it's restricted to 2 decimal places. – Jyreeil Mar 06 '20 at 04:52
  • Oh crap, it worked, I really appreciate what you did for me man, I really do :), thank you for helping me out, with my problem – Ulric10 Mar 06 '20 at 20:13