diff options
author | Riccardo Spagni <ric@spagni.net> | 2017-03-03 14:31:04 +0200 |
---|---|---|
committer | Riccardo Spagni <ric@spagni.net> | 2017-03-03 14:31:04 +0200 |
commit | d35d6261810bea652e526b4c93e6dcf063c498eb (patch) | |
tree | 4dc30d32c47f6a2e7254b62a989876c839317440 /src/cryptonote_basic | |
parent | Merge pull request #1823 (diff) | |
parent | allow user I/O in millinero, micronero, nanonero, piconero (diff) | |
download | monero-d35d6261810bea652e526b4c93e6dcf063c498eb.tar.xz |
Merge pull request #1826
2c468dd4 allow user I/O in millinero, micronero, nanonero, piconero (moneromooo-monero)
Diffstat (limited to 'src/cryptonote_basic')
-rw-r--r-- | src/cryptonote_basic/cryptonote_format_utils.cpp | 64 | ||||
-rw-r--r-- | src/cryptonote_basic/cryptonote_format_utils.h | 5 |
2 files changed, 60 insertions, 9 deletions
diff --git a/src/cryptonote_basic/cryptonote_format_utils.cpp b/src/cryptonote_basic/cryptonote_format_utils.cpp index 6baa57d26..05b24eee0 100644 --- a/src/cryptonote_basic/cryptonote_format_utils.cpp +++ b/src/cryptonote_basic/cryptonote_format_utils.cpp @@ -31,6 +31,7 @@ #include "include_base_utils.h" using namespace epee; +#include <atomic> #include "cryptonote_format_utils.h" #include "cryptonote_config.h" #include "crypto/crypto.h" @@ -65,6 +66,8 @@ static const uint64_t valid_decomposed_outputs[] = { (uint64_t)10000000000000000000ull }; +static std::atomic<unsigned int> default_decimal_point(CRYPTONOTE_DISPLAY_DECIMAL_POINT); + namespace cryptonote { //--------------------------------------------------------------- @@ -152,12 +155,12 @@ namespace cryptonote if (std::string::npos != point_index) { fraction_size = str_amount.size() - point_index - 1; - while (CRYPTONOTE_DISPLAY_DECIMAL_POINT < fraction_size && '0' == str_amount.back()) + while (default_decimal_point < fraction_size && '0' == str_amount.back()) { str_amount.erase(str_amount.size() - 1, 1); --fraction_size; } - if (CRYPTONOTE_DISPLAY_DECIMAL_POINT < fraction_size) + if (default_decimal_point < fraction_size) return false; str_amount.erase(point_index, 1); } @@ -169,9 +172,9 @@ namespace cryptonote if (str_amount.empty()) return false; - if (fraction_size < CRYPTONOTE_DISPLAY_DECIMAL_POINT) + if (fraction_size < default_decimal_point) { - str_amount.append(CRYPTONOTE_DISPLAY_DECIMAL_POINT - fraction_size, '0'); + str_amount.append(default_decimal_point - fraction_size, '0'); } return string_tools::get_xtype_from_string(amount, str_amount); @@ -514,14 +517,59 @@ namespace cryptonote cn_fast_hash(blob.data(), blob.size(), res); } //--------------------------------------------------------------- - std::string print_money(uint64_t amount) + void set_default_decimal_point(unsigned int decimal_point) + { + switch (decimal_point) + { + case 12: + case 9: + case 6: + case 3: + case 0: + default_decimal_point = decimal_point; + break; + default: + ASSERT_MES_AND_THROW("Invalid decimal point specificatin: " << decimal_point); + } + } + //--------------------------------------------------------------- + unsigned int get_default_decimal_point() + { + return default_decimal_point; + } + //--------------------------------------------------------------- + std::string get_unit(unsigned int decimal_point) + { + if (decimal_point == (unsigned int)-1) + decimal_point = default_decimal_point; + switch (std::atomic_load(&default_decimal_point)) + { + case 12: + return "monero"; + case 9: + return "millinero"; + case 6: + return "micronero"; + case 3: + return "nanonero"; + case 0: + return "piconero"; + default: + ASSERT_MES_AND_THROW("Invalid decimal point specificatin: " << default_decimal_point); + } + } + //--------------------------------------------------------------- + std::string print_money(uint64_t amount, unsigned int decimal_point) { + if (decimal_point == (unsigned int)-1) + decimal_point = default_decimal_point; std::string s = std::to_string(amount); - if(s.size() < CRYPTONOTE_DISPLAY_DECIMAL_POINT+1) + if(s.size() < decimal_point+1) { - s.insert(0, CRYPTONOTE_DISPLAY_DECIMAL_POINT+1 - s.size(), '0'); + s.insert(0, decimal_point+1 - s.size(), '0'); } - s.insert(s.size() - CRYPTONOTE_DISPLAY_DECIMAL_POINT, "."); + if (decimal_point > 0) + s.insert(s.size() - decimal_point, "."); return s; } //--------------------------------------------------------------- diff --git a/src/cryptonote_basic/cryptonote_format_utils.h b/src/cryptonote_basic/cryptonote_format_utils.h index b527502ac..7b09235b8 100644 --- a/src/cryptonote_basic/cryptonote_format_utils.h +++ b/src/cryptonote_basic/cryptonote_format_utils.h @@ -103,7 +103,10 @@ namespace cryptonote uint64_t get_block_height(const block& b); std::vector<uint64_t> relative_output_offsets_to_absolute(const std::vector<uint64_t>& off); std::vector<uint64_t> absolute_output_offsets_to_relative(const std::vector<uint64_t>& off); - std::string print_money(uint64_t amount); + void set_default_decimal_point(unsigned int decimal_point = CRYPTONOTE_DISPLAY_DECIMAL_POINT); + unsigned int get_default_decimal_point(); + std::string get_unit(unsigned int decimal_point = -1); + std::string print_money(uint64_t amount, unsigned int decimal_point = -1); //--------------------------------------------------------------- template<class t_object> bool t_serializable_object_to_blob(const t_object& to, blobdata& b_blob) |