3 Replies Latest reply on Jan 6, 2011 6:09 AM by mlavie

    UnitValue math

    linx-eric

      var margin = new UnitValue('1in');
      var span = doc.documentPreferences.pageWidth - (2*margin);

       

      I thought the above would work out ok, but it didn't.  Here's output from the Javascript Console:

      doc.documentPreferences.pageWidth
      Result: 120
      margin
      Result: 1 in
      2*margin
      Result: 2 in
      doc.documentPreferences.pageWidth - (2*margin)
      Result: -118 in
      doc.documentPreferences.pageWidth + (-2*margin)
      Result: 118 in

       


        • 1. Re: UnitValue math
          [Jongware] Most Valuable Participant

          I've had loads of problems using UnitValue, and have all but given up on that. I would guess the underlying problem is that JS doesn't really have a concept of "a number as measurement", so -- probably -- the result is a simple string. And you cannot do

           

          var a = 2 * "1 in";

           

          to get the output

           

          $.writeln(a) -> "2 in"

           

          Search this forum; I think I saw a workaround a while ago, using a small function to convert to and from different measurement units.

          • 2. Re: UnitValue math
            Marc Autret Level 4

            Sounds as a very serious bug!

             

            It all seems as if UnitValue reverses the subtraction operands when the first term is a Number:

             

            // tested in ID CS4 and CS5
            
            var r = Number(10) - UnitValue(0,'in');
            
            alert(r);          // => -10 in
            alert(r.__class__);     // => UnitValue
            alert(r.value);          // => -10
            alert(r.type);          // => 'in'
            

             

            Interestingly, we also get:

             

            // tested in ID CS4 and CS5
            
            alert( 10 - UnitValue() );  // => UnitValue -10.000000
            alert( -10 - UnitValue() ); // => UnitValue 10.000000
            

             

            Crazy!

             

            Thanks for pointing that out, linx-eric.

             

            @+

            Marc

            • 3. Re: UnitValue math
              mlavie Level 1

              Is there an ActionScipt equiv. of the JavaScript UnitValue ?