From 11cbd42220fb566f02605c2ff952d362014f3303 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Mon, 9 Feb 2015 12:34:31 -0800 Subject: [PATCH] Cleanups for ether denominations. --- libethcore/CommonEth.cpp | 61 ++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/libethcore/CommonEth.cpp b/libethcore/CommonEth.cpp index 7153cd9eb..f71969525 100644 --- a/libethcore/CommonEth.cpp +++ b/libethcore/CommonEth.cpp @@ -35,35 +35,42 @@ namespace eth const unsigned c_protocolVersion = 53; const unsigned c_databaseVersion = 5; -template constexpr u256 eth_unit() { return eth_unit() * u256(1000); } -template <> constexpr u256 eth_unit<0>() { return u256(1); } +template constexpr u256 exp10() +{ + return exp10() * u256(10); +} -static const vector> g_units = +template <> constexpr u256 exp10<0>() { - {eth_unit<18>(), "Uether"}, - {eth_unit<17>(), "Vether"}, - {eth_unit<16>(), "Dether"}, - {eth_unit<15>(), "Nether"}, - {eth_unit<14>(), "Yether"}, - {eth_unit<13>(), "Zether"}, - {eth_unit<12>(), "Eether"}, - {eth_unit<11>(), "Pether"}, - {eth_unit<10>(), "Tether"}, - {eth_unit<9>(), "Gether"}, - {eth_unit<8>(), "Mether"}, - {eth_unit<7>(), "grand"}, - {eth_unit<6>(), "ether"}, - {eth_unit<5>(), "finney"}, - {eth_unit<4>(), "szabo"}, - {eth_unit<3>(), "Gwei"}, - {eth_unit<2>(), "Mwei"}, - {eth_unit<1>(), "Kwei"}, - {eth_unit<0>(), "wei"} -}; + return u256(1); +} vector> const& units() { - return g_units; + static const vector> s_units = + { + {exp10<54>(), "Uether"}, + {exp10<51>(), "Vether"}, + {exp10<48>(), "Dether"}, + {exp10<45>(), "Nether"}, + {exp10<42>(), "Yether"}, + {exp10<39>(), "Zether"}, + {exp10<36>(), "Eether"}, + {exp10<33>(), "Pether"}, + {exp10<30>(), "Tether"}, + {exp10<27>(), "Gether"}, + {exp10<24>(), "Mether"}, + {exp10<21>(), "grand"}, + {exp10<18>(), "ether"}, + {exp10<15>(), "finney"}, + {exp10<12>(), "szabo"}, + {exp10<9>(), "Gwei"}, + {exp10<6>(), "Mwei"}, + {exp10<3>(), "Kwei"}, + {exp10<0>(), "wei"} + }; + + return s_units; } std::string formatBalance(bigint const& _b) @@ -78,13 +85,13 @@ std::string formatBalance(bigint const& _b) else b = (u256)_b; - if (b > g_units[0].first * 10000) + if (b > units()[0].first * 10000) { - ret << (b / g_units[0].first) << " " << g_units[0].second; + ret << (b / units()[0].first) << " " << units()[0].second; return ret.str(); } ret << setprecision(5); - for (auto const& i: g_units) + for (auto const& i: units()) if (i.first != 1 && b >= i.first * 100) { ret << (double(b / (i.first / 1000)) / 1000.0) << " " << i.second;