7#if defined(__cpp_lib_format)
18 || std::is_same_v<T, int128_t> || std::is_same_v<T, int256_t>;
20 template <decimal_
integer_type T>
35 explicit operator float() const
37 explicit operator
double() const
39 explicit operator
long double() const
43 [[nodiscard]] explicit operator
std::
string() const
48 [[nodiscard]]
int scale() const;
52 template <class FLOAT_TYPE>
53 [[nodiscard]] FLOAT_TYPE convert_to_floating_point() const
75 template <decimal_
integer_type T>
83 template <decimal_
integer_type T>
90 template <decimal_
integer_type T>
93 return m_value == other.m_value && m_scale == other.m_scale;
96 template <decimal_
integer_type T>
99 return !(*
this == other);
102 template <decimal_
integer_type T>
106 return convert_to_floating_point<float>();
109 template <decimal_
integer_type T>
113 return convert_to_floating_point<double>();
116 template <decimal_
integer_type T>
120 return convert_to_floating_point<long double>();
123 template <decimal_
integer_type T>
127 std::stringstream ss;
129 std::string result = ss.str();
134 if (result[0] ==
'0')
139 if (result[0] ==
'-')
141 result = result.substr(1);
146 if (result.length() <=
static_cast<std::size_t
>(m_scale))
150 std::string(
static_cast<std::size_t
>(m_scale) + 1 - result.length(),
'0')
153 std::size_t int_part_len = result.length() -
static_cast<std::size_t
>(m_scale);
154 std::string int_part = result.substr(0, int_part_len);
155 std::string frac_part = result.substr(int_part_len);
156 result = int_part +
"." + frac_part;
160 result += std::string(
static_cast<std::size_t
>(-m_scale),
'0');
165 result.insert(0, 1,
'-');
170 template <decimal_
integer_type T>
176 template <decimal_
integer_type T>
182 template <decimal_
integer_type T>
183 template <
class FLOAT_TYPE>
184 FLOAT_TYPE decimal<T>::convert_to_floating_point() const
187 using to_type = FLOAT_TYPE;
188 if constexpr (std::is_same_v<T, int256_t>)
191 auto val =
static_cast<int128_t>(m_value);
192 return static_cast<to_type
>(val) /
static_cast<to_type
>(std::pow(10, m_scale));
196 return static_cast<to_type
>(m_value) /
static_cast<to_type
>(std::pow(10, m_scale));
202#if defined(__cpp_lib_format)
205struct std::formatter<
sparrow::decimal<T>>
207 constexpr auto parse(std::format_parse_context& ctx)
212 auto format(
const sparrow::decimal<T>& d, std::format_context& ctx)
const
214 return std::format_to(ctx.out(),
"Decimal({}, {})", d.
storage(), d.
scale());
221 os << std::format(
"{}", value);
bool operator!=(const decimal &other) const
bool operator==(const decimal &other) const
decimal(T value, int scale)
const T & storage() const
primesum::int128_t int128_t
constexpr bool is_int_placeholder_v
constexpr bool is_decimal_v
std::ostream & operator<<(std::ostream &os, const sparrow::nullval_t &)