From 3e91ddc6d736e699fcbaeb219ddf1b58fc81ddeb Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 9 Apr 2012 03:59:36 -0700 Subject: [PATCH] Currency work. --- src/Currency.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/Currency.cpp diff --git a/src/Currency.cpp b/src/Currency.cpp new file mode 100644 index 0000000000..34a9d03fa2 --- /dev/null +++ b/src/Currency.cpp @@ -0,0 +1,56 @@ + +#include "Currency.h" + +#include +#include + +uint160 Currency::sNatMask("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000"); +uint160 Currency::sNatZero("FFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000"); +uint64 Amount::sMaxCanon(1ull << 63); + +Currency::Currency(const uint160& v) : mValue(v) +{ + if (!v) mType = ctNATIVE; + if (!(v & sNatZero)) mType = ctNATIONAL; + mType = ctNATIONAL; +} + +bool Currency::isCommensurate(const Currency& c) const +{ + if (isNative()) + return c.isNative(); + if (isCustom()) + return mValue == c.mValue; + if (!c.isNational()) return false; + return (mValue & sNatMask) == (c.mValue & sNatMask); +} + +unsigned char Currency::getScale() const +{ + return *(mValue.begin()); +} + +void Currency::setScale(unsigned char s) +{ + *(mValue.begin()) = s; +} + +void Amount::canonicalize() +{ // clear high bit to avoid overflows + if(mQuantity > sMaxCanon) + { + if (!mCurrency.isNational()) throw std::runtime_error("Currency overflow"); + unsigned char s = mCurrency.getScale(); + if (s==255) throw std::runtime_error("Currency overflow"); + mCurrency.setScale(s + 1); + mQuantity /= 10.0; + } +} + +double Amount::getDisplayQuantity() const +{ + if(!mCurrency.isNational()) throw std::runtime_error("Can only scale national currencies"); + int scale=mCurrency.getScale(); + return static_cast(mQuantity) * pow(10, 128-scale); +} +