8#if defined(__cpp_lib_format)
32 explicit operator float() const
34 explicit operator
double() const
36 explicit operator
long double() const
40 [[nodiscard]] explicit operator
std::
string() const
45 [[nodiscard]]
int scale() const;
49 template <class FLOAT_TYPE>
50 [[nodiscard]] FLOAT_TYPE convert_to_floating_point() const
87 return m_value == other.m_value && m_scale == other.m_scale;
93 return !(*
this == other);
100 return convert_to_floating_point<float>();
103 template <
typename T>
107 return convert_to_floating_point<double>();
110 template <
typename T>
114 return convert_to_floating_point<long double>();
117 template <
typename T>
121 std::stringstream ss;
123 std::string result = ss.str();
128 if (result[0] ==
'0')
133 if (result[0] ==
'-')
135 result = result.substr(1);
140 if (result.length() <=
static_cast<std::size_t
>(m_scale))
144 std::string(
static_cast<std::size_t
>(m_scale) + 1 - result.length(),
'0')
147 std::size_t int_part_len = result.length() -
static_cast<std::size_t
>(m_scale);
148 std::string int_part = result.substr(0, int_part_len);
149 std::string frac_part = result.substr(int_part_len);
150 result = int_part +
"." + frac_part;
154 result += std::string(
static_cast<std::size_t
>(-m_scale),
'0');
159 result.insert(0, 1,
'-');
164 template <
typename T>
170 template <
typename T>
176 template <
typename T>
177 template <
class FLOAT_TYPE>
178 FLOAT_TYPE decimal<T>::convert_to_floating_point() const
181 using to_type = FLOAT_TYPE;
182 if constexpr (std::is_same_v<T, int256_t>)
185 auto val =
static_cast<int128_t>(m_value);
186 return static_cast<to_type
>(val) /
static_cast<to_type
>(std::pow(10, m_scale));
190 return static_cast<to_type
>(m_value) /
static_cast<to_type
>(std::pow(10, m_scale));
196#if defined(__cpp_lib_format)
199struct std::formatter<
sparrow::decimal<T>>
201 constexpr auto parse(std::format_parse_context& ctx)
206 auto format(
const sparrow::decimal<T>& d, std::format_context& ctx)
const
208 return std::format_to(ctx.out(),
"Decimal({}, {})", d.
storage(), d.
scale());
215 os << std::format(
"{}", value);
bool operator!=(const decimal &other) const
std::int32_t integer_type
bool operator==(const decimal &other) const
decimal(T value, int scale)
const std::int32_t & storage() const
primesum::int128_t int128_t
constexpr bool is_int_placeholder_v
std::ostream & operator<<(std::ostream &stream, T n)
constexpr bool is_decimal_v