4 Replies Latest reply on Mar 18, 2008 12:34 PM by Chuck1411

    Math.abs() not working

    Chuck1411 Level 1
      I have the code:
      // return value to 4 decimal places
      private function truncateValue
      var returnValue:Number = Math.abs( (value * 10000) );
      returnValue /= 10000;
      return returnValue;

      This apparently is having no effect upon the passed in value. I still get precision out to ten decimal places and it overruns my text box.

      And where is "Attach Code"?
        • 1. Re: Math.abs() not working
          Gregory Lafrance Level 6
          I don't see how you would expect return value to 4 decimal places. Should you be dividing by 1000?

          <?xml version="1.0"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">
          private function truncateValue(value:Number):Number{
          var returnValue:Number = Math.abs( (value * 10000) );
          returnValue /= 10000;
          return returnValue;
          <mx:Button id="btn1" label="Click Me" click="btn1.label=String(truncateValue(5))"/>
          <mx:Button id="btn2" label="Click Me" click="btn2.label=String(truncateValue(-5))"/>
          <mx:Button id="btn3" label="Click Me" click="btn3.label=String(truncateValue(.5))"/>
          <mx:Button id="btn4" label="Click Me" click="btn4.label=String(truncateValue(-.5))"/>
          <mx:Button id="btn5" label="Click Me" click="btn5.label=String(truncateValue(.005))"/>
          <mx:Button id="btn6" label="Click Me" click="btn6.label=String(truncateValue(-.005))"/>
          <mx:Button id="btn7" label="Click Me" click="btn7.label=String(truncateValue(.12345678901234567890))"/>
          <mx:Button id="btn8" label="Click Me" click="btn8.label=String(truncateValue(-.12345678901234567890))"/>
          • 2. Re: Math.abs() not working
            Chuck1411 Level 1
            I ended up using Number().ToPrecision(), but that isn't what I wanted. I may have the math wrong, but the fact that abs() being passed a value of 10.0005 returns 10.0005 is troubling. Or I was doing something wrong, in my first day in Flex. I was using it for testing some things as I began working my application, I no longer use the code since I switched to ToPrecision().
            • 3. Re: Math.abs() not working
              Math.abs just returns a absolute number. So if you gave it -10.0005, you'd get 10.0005..

              toPrecision or Math.round are probably more up your alley.. And as Greg said, to move decimals the easiest/fastest way is dividing/multiplying by 10,100, etc

              round nearest 4 decimals..

              var num = 10.345667
              var numRound = num * 10000 // 103456.67
              numRound = Math.round(numRound); // 103457
              num = numRound / 10000; //10.3457
              • 4. Re: Math.abs() not working
                Chuck1411 Level 1
                Thanks, I guess I had abs() in my head as a truncate kind of function, thanks for the clarification.