Skip navigation
linx-eric
Currently Being Moderated

UnitValue math

Dec 15, 2010 1:20 PM

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

 


  • Currently Being Moderated
    Community Member
    Dec 15, 2010 2:11 PM

    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.

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Dec 15, 2010 9:05 PM

    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

    |
    Mark as:
  • Currently Being Moderated
    Community Member
    Jan 6, 2011 7:09 AM

    Is there an ActionScipt equiv. of the JavaScript UnitValue ?

    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)