summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrzej Hunt <andrzej@ahunt.org>2015-03-15 19:59:16 +0000
committerAndrzej Hunt <andrzej@ahunt.org>2015-10-20 18:18:22 +0200
commit165dcf7a4d80cf62544e38351456f9f0a992ef76 (patch)
treea00b85d376468f6db8cf9f91097179d3fb5dda2c
parent454f36653297c2e9ec24328734b798374f004a13 (diff)
Implement UtUnit::convertValueTo
Change-Id: Id4469c7f03b3d73a4d2f7c4582602fed64b47df7
-rw-r--r--sc/qa/unit/units.cxx8
-rw-r--r--sc/source/core/units/utunit.hxx15
2 files changed, 23 insertions, 0 deletions
diff --git a/sc/qa/unit/units.cxx b/sc/qa/unit/units.cxx
index 32d48d78213e..c657981bb782 100644
--- a/sc/qa/unit/units.cxx
+++ b/sc/qa/unit/units.cxx
@@ -110,6 +110,14 @@ void UnitsTest::testUTUnit() {
CPPUNIT_ASSERT(aM_S*aS == aM);
CPPUNIT_ASSERT(aM/aS == aM_S);
+
+ // Do some simple conversion testing
+ UtUnit aCM;
+ UtUnit::createUnit("cm", aCM, mpUnitsImpl->mpUnitSystem);
+ CPPUNIT_ASSERT(aCM.areConvertibleTo(aM));
+ CPPUNIT_ASSERT(!aCM.areConvertibleTo(aS));
+ CPPUNIT_ASSERT(aCM.convertValueTo(0.0, aM) == 0.0); // 0 converts to 0
+ CPPUNIT_ASSERT(aCM.convertValueTo(100.0, aM) == 1.0);
}
void UnitsTest::testUnitVerification() {
diff --git a/sc/source/core/units/utunit.hxx b/sc/source/core/units/utunit.hxx
index 557a5fb84509..058891a91d4d 100644
--- a/sc/source/core/units/utunit.hxx
+++ b/sc/source/core/units/utunit.hxx
@@ -99,6 +99,21 @@ public:
bool areConvertibleTo(const UtUnit& rUnit) {
return ut_are_convertible(this->get(), rUnit.get());
}
+
+ double convertValueTo(double nOriginalValue, const UtUnit& rUnit) {
+ // We could write our own cv_converter wrapper too, but that
+ // seems unnecessary given the limited selection of
+ // operations (convert float/double, either individually
+ // or as an array) -- of which we only need a subset anyway
+ // (ie. only for doubles).
+ cv_converter* pConverter = ut_get_converter(this->get(), rUnit.get());
+ assert(pConverter);
+
+ float nConvertedValue = cv_convert_double(pConverter, nOriginalValue);
+
+ cv_free(pConverter);
+ return nConvertedValue;
+ }
};
template< typename charT, typename traits >