Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
asset.cpp
Go to the documentation of this file.
2#include <boost/rational.hpp>
4
5namespace sysio { namespace chain {
6
8 return sym.decimals();
9}
10
11string asset::symbol_name()const {
12 return sym.name();
13}
14
16 return sym.precision();
17}
18
19string asset::to_string()const {
20 string sign = amount < 0 ? "-" : "";
21 int64_t abs_amount = std::abs(amount);
22 string result = fc::to_string( static_cast<int64_t>(abs_amount) / precision());
23 if( decimals() )
24 {
25 auto fract = static_cast<int64_t>(abs_amount) % precision();
26 result += "." + fc::to_string(precision() + fract).erase(0,1);
27 }
28 return sign + result + " " + symbol_name();
29}
30
31asset asset::from_string(const string& from)
32{
33 try {
34 string s = fc::trim(from);
35
36 // Find space in order to split amount and symbol
37 auto space_pos = s.find(' ');
38 SYS_ASSERT((space_pos != string::npos), asset_type_exception, "Asset's amount and symbol should be separated with space");
39 auto symbol_str = fc::trim(s.substr(space_pos + 1));
40 auto amount_str = s.substr(0, space_pos);
41
42 // Ensure that if decimal point is used (.), decimal fraction is specified
43 auto dot_pos = amount_str.find('.');
44 if (dot_pos != string::npos) {
45 SYS_ASSERT((dot_pos != amount_str.size() - 1), asset_type_exception, "Missing decimal fraction after decimal point");
46 }
47
48 // Parse symbol
49 string precision_digit_str;
50 if (dot_pos != string::npos) {
51 precision_digit_str = sysio::chain::to_string(amount_str.size() - dot_pos - 1);
52 } else {
53 precision_digit_str = "0";
54 }
55
56 string symbol_part = precision_digit_str + ',' + symbol_str;
57 symbol sym = symbol::from_string(symbol_part);
58
59 // Parse amount
60 safe<int64_t> int_part, fract_part;
61 if (dot_pos != string::npos) {
62 int_part = fc::to_int64(amount_str.substr(0, dot_pos));
63 fract_part = fc::to_int64(amount_str.substr(dot_pos + 1));
64 if (amount_str[0] == '-') fract_part *= -1;
65 } else {
66 int_part = fc::to_int64(amount_str);
67 }
68
69 safe<int64_t> amount = int_part;
70 amount *= safe<int64_t>(sym.precision());
71 amount += fract_part;
72
73 return asset(amount.value, sym);
74 }
76}
77
78} } // sysio::types
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
string name() const
Definition symbol.hpp:104
uint8_t decimals() const
Definition symbol.hpp:93
uint64_t precision() const
Definition symbol.hpp:94
static symbol from_string(const string &from)
Definition symbol.hpp:68
#define FC_CAPTURE_LOG_AND_RETHROW(...)
fc::string to_string(double)
Definition string.cpp:131
fc::string trim(const fc::string &)
Definition string.cpp:152
int64_t to_int64(const fc::string &)
Definition string.cpp:92
signed __int64 int64_t
Definition stdint.h:135
unsigned char uint8_t
Definition stdint.h:124
T value
Definition safe.hpp:23
string symbol_name() const
Definition asset.cpp:11
string to_string() const
Definition asset.cpp:19
int64_t precision() const
Definition asset.cpp:15
uint8_t decimals() const
Definition asset.cpp:7
static asset from_string(const string &from)
Definition asset.cpp:31
asset(share_type a=0, symbol id=symbol(CORE_SYMBOL))
Definition asset.hpp:21
char * s