#include <cstdint>
#include <cmath>
#include <string>
#include <cstdio>
#include <cassert>
struct MyCurrency
{
uint64_t m_significand;
int32_t m_exp10;
bool m_sign;
MyCurrency()
: m_significand(0)
, m_exp10(0)
, m_sign(false)
{
}
void normalize()
{
if (m_significand == 0)
{
m_exp10 = 0;
m_sign = false;
return;
}
while (m_significand % 10 == 0)
{
m_significand /= 10;
++m_exp10;
}
}
MyCurrency(int64_t significand, int32_t exp10 = 0)
: m_significand(std::abs(significand))
, m_exp10(exp10)
, m_sign(significand < 0)
{
normalize();
}
MyCurrency(const char *str)
: m_significand(0)
, m_exp10(0)
, m_sign(false)
{
while (*str == ' ')
++str;
if (*str == '-')
{
m_sign = true;
++str;
}
else if (*str == '+')
{
++str;
}
while (*str == '0')
++str;
for (bool found_dot = false; *str; ++str)
{
if (*str == '.')
{
found_dot = true;
continue;
}
if (found_dot)
{
--m_exp10;
}
if ('0' <= *str && *str <= '9')
{
m_significand *= 10;
m_significand += *str - '0';
}
}
normalize();
}
MyCurrency(const std::string& str)
: MyCurrency(str.c_str())
{
}
MyCurrency operator+() const
{
return *this;
}
MyCurrency operator-() const
{
MyCurrency ret = *this;
ret.m_sign = !ret.m_sign;
return ret;
}
bool is_zero() const
{
return m_significand == 0;
}
bool is_positive() const
{
return !m_sign;
}
bool is_negative() const
{
return m_sign;
}
std::string to_string() const
{
if (is_zero())
return "0";
if (m_sign)
{
std::string ret("-");
ret += (-*this).to_string();
return ret;
}
std::string ret = std::to_string(m_significand);
if (m_exp10 == 0)
return ret; // just a integer
if (m_exp10 > 0)
{
std::string zeros(m_exp10, '0');
ret += zeros;
return ret; // 123456000...0
}
else if (-m_exp10 < ret.size())
{
// 1234.567...
ret.insert(ret.size() + m_exp10, ".");
return ret;
}
else
{
// 0.0123456...
std::string str = "0.";
std::string zeros(-m_exp10 - ret.size(), '0');
str += zeros + ret;
return str;
}
}
void print() const
{
std::printf("%s\n", to_string().c_str());
}
void round(int32_t e10 = 0)
{
// TODO:
}
void round_up(int32_t e10 = 0)
{
// TODO:
}
void round_down(int32_t e10 = 0)
{
// TODO:
}
void bankers_rounding(int32_t e10 = 0)
{
// TODO:
}
MyCurrency get_round(int32_t e10 = 0)
{
MyCurrency ret(*this);
ret.round(e10);
return ret;
}
MyCurrency get_round_up(int32_t e10 = 0)
{
MyCurrency ret(*this);
ret.round_up(e10);
return ret;
}
MyCurrency get_round_down(int32_t e10 = 0)
{
MyCurrency ret(*this);
ret.round_down(e10);
return ret;
}
MyCurrency get_bankers_rounding(int32_t e10)
{
MyCurrency ret(*this);
ret.get_bankers_rounding(e10);
return ret;
}
};
int main(void)
{
MyCurrency(0, 0).print();
MyCurrency(1, 2).print();
MyCurrency(10, -2).print();
MyCurrency(10, -3).print();
MyCurrency(12345, 0).print();
MyCurrency(12345, 10).print();
MyCurrency(12345, 2).print();
MyCurrency(12345, -2).print();
MyCurrency(-12345, -10).print();
MyCurrency("0").print();
MyCurrency("100").print();
MyCurrency("0.1").print();
MyCurrency("0.01").print();
MyCurrency("12345").print();
MyCurrency("123450000000000").print();
MyCurrency("1234500").print();
MyCurrency("123.45").print();
MyCurrency("-0.0000012345").print();
}