SHA256
80 lines
2.5 KiB
C++
80 lines
2.5 KiB
C++
#ifndef BETTERSTL_BIG_DECIMAL_H
|
|
#define BETTERSTL_BIG_DECIMAL_H
|
|
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <bstl/big_integer.hpp>
|
|
|
|
namespace bstl {
|
|
class BigDecimal {
|
|
public:
|
|
BigDecimal();
|
|
BigDecimal(int value);
|
|
BigDecimal(long long value);
|
|
BigDecimal(double value);
|
|
explicit BigDecimal(const std::string& value);
|
|
explicit BigDecimal(const char* value);
|
|
BigDecimal(const BigInteger& unscaledValue, int scale);
|
|
BigDecimal(const BigDecimal& other) = default;
|
|
BigDecimal& operator=(const BigDecimal& other) = default;
|
|
|
|
bool isZero() const;
|
|
int sign() const;
|
|
int getScale() const;
|
|
BigInteger getUnscaledValue() const;
|
|
std::string toString() const;
|
|
|
|
// Comparison operators
|
|
bool operator==(const BigDecimal& other) const;
|
|
bool operator!=(const BigDecimal& other) const;
|
|
bool operator<(const BigDecimal& other) const;
|
|
bool operator>(const BigDecimal& other) const;
|
|
bool operator<=(const BigDecimal& other) const;
|
|
bool operator>=(const BigDecimal& other) const;
|
|
|
|
bool operator==(int value) const;
|
|
bool operator!=(int value) const;
|
|
bool operator<(int value) const;
|
|
bool operator>(int value) const;
|
|
bool operator<=(int value) const;
|
|
bool operator>=(int value) const;
|
|
|
|
bool operator==(double value) const;
|
|
bool operator!=(double value) const;
|
|
bool operator<(double value) const;
|
|
bool operator>(double value) const;
|
|
bool operator<=(double value) const;
|
|
bool operator>=(double value) const;
|
|
|
|
// Arithmetic operators
|
|
BigDecimal operator+() const;
|
|
BigDecimal operator-() const;
|
|
BigDecimal& operator+=(const BigDecimal& other);
|
|
BigDecimal& operator-=(const BigDecimal& other);
|
|
BigDecimal& operator*=(const BigDecimal& other);
|
|
BigDecimal& operator/=(const BigDecimal& other);
|
|
|
|
BigDecimal operator+(const BigDecimal& other) const;
|
|
BigDecimal operator-(const BigDecimal& other) const;
|
|
BigDecimal operator*(const BigDecimal& other) const;
|
|
BigDecimal operator/(const BigDecimal& other) const;
|
|
|
|
BigDecimal abs() const;
|
|
BigDecimal round(int scale) const;
|
|
|
|
private:
|
|
BigInteger unscaledValue_; // 去掉小数点后的值
|
|
int scale_; // 小数点后的位数
|
|
|
|
static void alignScale(BigInteger& lhs, int& lhsScale,
|
|
BigInteger& rhs, int& rhsScale);
|
|
};
|
|
|
|
// 流运算符重载
|
|
std::ostream& operator<<(std::ostream& os, const BigDecimal& bd);
|
|
std::istream& operator>>(std::istream& is, BigDecimal& bd);
|
|
|
|
} // namespace bstl
|
|
|
|
#endif //BETTERSTL_BIG_DECIMAL_H
|