📄 currency.h
字号:
#ifndef __DNC_CURRENCY_H__
#define __DNC_CURRENCY_H__
//##############################################################################
//The dnc Library
//Copyright (c) 2005 Dreamsoft 赵纯华
//Last update: 2006-3-4
//提供货币类型
//##############################################################################
//
#ifndef __DNC_DEFINE_H__
#include "define.h"
#endif
#ifndef __DNC_CFUNC_H__
#include "cfunc.h"
#endif
#include <istream>
#include <ostream>
#include <string>
namespace dnc{
class DNC_DECLARE Currency{
//friend DNC_DECLARE std::ostream & operator <<(std::ostream &os,const Currency &obj);
public:
Currency(){
m_val = 0;
}
Currency(double v){
m_val = (int64)(v*10000);
}
Currency(const Decimal &v){
*this = v;
}
Currency(int64 v){
m_val = v*10000;
}
Currency(uint64 v){
m_val = (int64)(v*10000);
}
//参数:
// v 基本值
// scale 把v放大的倍数
Currency(int64 v,int scale){
m_val = v*scale;
}
public:
inline operator const double() const{
return m_val/10000.0;
}
inline Currency& operator = (double v){
m_val =(int64)(v*10000);
return *this;
}
inline Currency& operator = (int64 v){
m_val = v*10000;
return *this;
}
inline Currency& operator = (uint64 v){
m_val = (int64)(v*10000);
return *this;
}
Currency& operator = (const Decimal &v);
//
//比较运算
//
inline bool operator == (const Currency &v) const{
return m_val == v.m_val;
}
inline bool operator < (const Currency &v) const{
return m_val<v.m_val;
}
inline bool operator != (const Currency &v) const{return !(*this == v);}
inline bool operator > (const Currency &v) const{return !(*this == v||*this<v);};
inline bool operator <= (const Currency &v) const{return *this==v || *this<v;};
inline bool operator >= (const Currency &v) const{return !(*this<v);};
inline bool operator == (int64 v) const{
return m_val == v*10000;
}
inline bool operator < (int64 v) const{
return m_val < v*10000;
}
inline bool operator != (int64 v) const{return !(*this == v);}
inline bool operator > (int64 v) const{return !(*this == v||*this<v);};
inline bool operator <= (int64 v) const{return *this==v || *this<v;};
inline bool operator >= (int64 v) const{return !(*this<v);};
inline bool operator == (uint64 v) const{
return m_val == (int64)v*10000;
}
inline bool operator < (uint64 v) const{
return m_val < (int64)v*10000;
}
inline bool operator != (uint64 v) const{return !(*this == v);}
inline bool operator > (uint64 v) const{return !(*this == v||*this<v);};
inline bool operator <= (uint64 v) const{return *this==v || *this<v;};
inline bool operator >= (uint64 v) const{return !(*this<v);};
inline bool operator == (double v) const{
return m_val == (int64)(v*10000);
}
inline bool operator < (double v) const{
return m_val < (int64)(v*10000);
}
inline bool operator != (double v) const{return !(*this == v);}
inline bool operator > (double v) const{return !(*this == v||*this<v);};
inline bool operator <= (double v) const{return *this==v || *this<v;};
inline bool operator >= (double v) const{return !(*this<v);};
bool operator == (const Decimal &v) const;
bool operator < (const Decimal &v) const;
inline bool operator != (const Decimal &v) const{return !(*this == v);}
inline bool operator > (const Decimal &v) const{return !(*this == v||*this<v);};
inline bool operator <= (const Decimal &v) const{return *this==v || *this<v;};
inline bool operator >= (const Decimal &v) const{return !(*this<v);};
//正负号
inline const Currency operator -() const{
return -m_val;
}
inline const Currency operator +() const{
return m_val;
}
//+ -
inline Currency& operator += (const Currency &v){
m_val += v.m_val;
return *this;
}
inline Currency& operator -= (const Currency &v){
m_val -= v.m_val;
return *this;
}
inline Currency& operator += (int64 v){
m_val += v*10000;
return *this;
}
inline Currency& operator -= (int64 v){
m_val -= v*10000;
return *this;
}
inline Currency& operator += (uint64 v){
m_val += (int64)(v*10000);
return *this;
}
inline Currency& operator -= (uint64 v){
m_val -= (int64)(v*10000);
return *this;
}
inline Currency& operator += (double v){
m_val += (int64)(v*10000);
return *this;
}
inline Currency& operator -= (double v){
m_val -= (int64)(v*10000);
return *this;
}
Currency& operator += (const Decimal &v);
Currency& operator -= (const Decimal &v);
std::string toString()const;
//分开整数部分和小数部分
inline void disjoin(int64 &integer,int64 &decimal)const{
integer = (int64)(m_val / 10000.0);
decimal = m_val - integer*10000;
while(!(decimal%10) && decimal!=0) decimal /= 10;
}
//连接整数部分和小数部分
inline void join(int64 integer,int64 decimal){
m_val = integer*10000;
m_val += decimal;
}
private:
int64 m_val;
};
inline const Currency operator+(Currency td,const Currency &de){return td+=de;}
inline const Currency operator-(Currency td,const Currency &de){return td-=de;}
inline const Currency operator+(Currency td,int64 de){return td+=de;}
inline const Currency operator-(Currency td,int64 de){return td-=de;}
inline const Currency operator+(Currency td,uint64 de){return td+=de;}
inline const Currency operator-(Currency td,uint64 de){return td-=de;}
inline const Currency operator+(Currency td,double de){return td+=de;}
inline const Currency operator-(Currency td,double de){return td-=de;}
inline const Currency operator+(Currency td,const Decimal &de){return td+=de;}
inline const Currency operator-(Currency td,const Decimal &de){return td-=de;}
inline const Currency operator+(int64 de,Currency td){return td+=de;}
inline const Currency operator-(int64 de,Currency td){return Currency(de)-=td;}
inline const Currency operator+(uint64 de,Currency td){return td+=de;}
inline const Currency operator-(uint64 de,Currency td){return Currency(de)-=td;}
inline const Currency operator+(double de,Currency td){return td+=de;}
inline const Currency operator-(double de,Currency td){return Currency(de)-=td;}
inline const Currency operator+(const Decimal &de,Currency td){return td+=de;}
inline const Currency operator-(const Decimal &de,Currency td){return Currency(de)-=td;}
#if 0
DNC_DECLARE std::ostream & operator <<(std::ostream &os,const Currency &obj);
DNC_DECLARE std::istream& operator>>(std::istream &is,Currency &val);
#else
template <class Elem, class Traits>
std::basic_ostream<Elem,Traits>& operator<<(
std::basic_ostream<Elem,Traits> &os,const Currency &cy)
{
int64 v1,v2;
cy.disjoin(v1,v2);
os<<v1<<Elem('.')<<v2;
return os;
}
template <class Elem, class Traits>
std::basic_istream<Elem,Traits>& operator>>(
std::basic_istream<Elem,Traits> &is,Currency &val)
{
Elem ch;
int64 v1,v2;
is>>v1>>ch>>v2;
val.join(v1,v2);
return is;
}
#endif
}
#endif //__DNC_CURRENCY_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -