Repository: josdejong/mathjs (js)

Issue:
Unit's .toNumeric() incorrect immediately after calculation with mixed units
When doing calculations with mixed units, such as multiplying feet and meters, .toNumeric() returns an incorrect value immediately after the calculation. Once .formatUnits() has been called on the result, .toNumeric() returns the correct value.  

An example at http://jsbin.com/wusavigugi/edit?html,js,output shows that 
```
    const a = math.unit(1, 'ft').multiply(math.unit(1, 'meter'))
    document.write(a.toNumeric() + '<br>');
    document.write(a.formatUnits() + '<br>');
    document.write(a.toNumeric() + '<br>');
```

outputs
```
1
meter^2
0.3048
```

Pull request description:
Simplify units before returning numeric value
This should close #901 . There is a separate issue breaking unit tests when run all at once, which I'll be filing shortly. 

Patch:
diff --git a/lib/type/unit/Unit.js b/lib/type/unit/Unit.js
index e1c05e24e..d3026813f 100644
--- a/lib/type/unit/Unit.js
+++ b/lib/type/unit/Unit.js
@@ -866,6 +866,8 @@ function factory (type, config, load, typed, math) {
       other = this.to(valuelessUnit);
     }
 
+    other.simplifyUnitListLazy();
+
     if(other._isDerived()) {
       return other._denormalize(other.value);
     }


Test patch:
diff --git a/test/type/unit/Unit.test.js b/test/type/unit/Unit.test.js
index 8587ad49b..8616abd12 100644
--- a/test/type/unit/Unit.test.js
+++ b/test/type/unit/Unit.test.js
@@ -252,6 +252,12 @@ describe('Unit', function() {
       var u = new Unit(math.fraction(1,3), 'cm');
       assert.deepEqual(u.toNumeric('mm'), math.fraction(10,3));
     });
+    it ('should simplify units before returning a numeric value', function () {
+      var cm = new Unit(1, 'gram');
+      var m = new Unit(1, 'kilogram');
+      var product = cm.multiply(m)
+      assert.deepEqual(product.toNumeric(), 0.001);
+    });
   });
 
   describe('to', function() {