Decimal is a high-precision arithmetic library for financial calculations. Mimics the behavior of Postgresql's decimal type.
- C++17 compliant compiler
- CMake (>= 3.28.3)
- GoogleTest (for running tests)
# build
cmake -S . -B build
cmake --build build
# run tests
./build/tests/TEST
#include <iostream>
#include "decimal.h"
int main() {
using utils::finantial::Decimal;
Decimal a{"123.456"};
Decimal b{"78.9"};
std::cout << "add: " << a + b << '\n';
std::cout << "sub: " << a - b << '\n';
std::cout << "mul: " << a * b << '\n';
std::cout << "div: " << a / b << '\n';
std::cout << "mod: " << a % b << '\n';
return 0;
}
namespace utils::finantial {
template <std::size_t I = 24, std::size_t F = 8> class Decimal final {
public:
Decimal(const std::string_view& value = {});
Decimal(const Decimal&) noexcept = default;
Decimal(Decimal&&) noexcept = default;
~Decimal() = default;
// Assignment
auto operator=(const std::string_view& value) -> Decimal&;
auto operator=(const Decimal&) noexcept -> Decimal& = default;
auto operator=(Decimal&&) noexcept -> Decimal& = default;
// Comparison operators
[[nodiscard]] auto operator<(const Decimal& other) const noexcept -> bool;
[[nodiscard]] auto operator>(const Decimal& other) const noexcept -> bool;
[[nodiscard]] auto operator>=(const Decimal& other) const noexcept -> bool;
[[nodiscard]] auto operator<=(const Decimal& other) const noexcept -> bool;
[[nodiscard]] auto operator==(const Decimal& other) const noexcept -> bool;
[[nodiscard]] auto operator!=(const Decimal& other) const noexcept -> bool;
// Arithmetic operators
[[nodiscard]] auto operator-() const noexcept -> Decimal;
[[nodiscard]] auto operator+(const Decimal& other) const noexcept -> Decimal;
[[nodiscard]] auto operator-(const Decimal& other) const noexcept -> Decimal;
[[nodiscard]] auto operator*(const Decimal& other) const noexcept -> Decimal;
[[nodiscard]] auto operator/(const Decimal& other) const noexcept -> Decimal;
[[nodiscard]] auto operator%(const Decimal& other) const noexcept -> Decimal;
auto operator+=(const Decimal& other) noexcept -> Decimal&;
auto operator-=(const Decimal& other) noexcept -> Decimal&;
auto operator*=(const Decimal& other) noexcept -> Decimal&;
auto operator/=(const Decimal& other) noexcept -> Decimal&;
auto operator%=(const Decimal& other) noexcept -> Decimal&;
// Stream output
template <std::size_t _I, std::size_t _F>
friend auto operator<<(std::ostream& os, Decimal<_I, _F> decimal) -> std::ostream&;
// Conversion to string
explicit operator std::string() const noexcept;
};
} // namespace utils::finantial
The library currently uses basic algorithms for computations. If you have experience with more efficient methods, especially those utilizing bitwise operations, you are welcome to contribute your improvements. This will not only enhance the library's performance but also help the author gain knowledge of these techniques.
This project is licensed under the MIT License.