forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
units_money.h
97 lines (75 loc) · 2.35 KB
/
units_money.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#pragma once
#ifndef CATA_SRC_UNITS_MONEY_H
#define CATA_SRC_UNITS_MONEY_H
#include <algorithm>
#include "units_def.h"
namespace units
{
class money_in_cent_tag
{
};
using money = quantity<int, money_in_cent_tag>;
const money money_min = units::money( std::numeric_limits<units::money::value_type>::min(),
units::money::unit_type{} );
const money money_max = units::money( std::numeric_limits<units::money::value_type>::max(),
units::money::unit_type{} );
template<typename value_type>
inline constexpr quantity<value_type, money_in_cent_tag> from_cent(
const value_type v )
{
return quantity<value_type, money_in_cent_tag>( v, money_in_cent_tag{} );
}
template<typename value_type>
inline constexpr quantity<value_type, money_in_cent_tag> from_usd( const value_type v )
{
return from_cent<value_type>( v * 100 );
}
template<typename value_type>
inline constexpr quantity<value_type, money_in_cent_tag> from_kusd( const value_type v )
{
return from_usd<value_type>( v * 1000 );
}
template<typename value_type>
inline constexpr value_type to_cent( const quantity<value_type, money_in_cent_tag> &v )
{
return v / from_cent<value_type>( 1 );
}
template<typename value_type>
inline constexpr value_type to_usd( const quantity<value_type, money_in_cent_tag> &v )
{
return to_cent( v ) / 100.0;
}
template<typename value_type>
inline constexpr value_type to_kusd( const quantity<value_type, money_in_cent_tag> &v )
{
return to_usd( v ) / 1000.0;
}
} // namespace units
inline constexpr units::money operator"" _cent( const unsigned long long v )
{
return units::from_cent( v );
}
inline constexpr units::quantity<double, units::money_in_cent_tag> operator"" _cent(
const long double v )
{
return units::from_cent( v );
}
inline constexpr units::money operator"" _USD( const unsigned long long v )
{
return units::from_usd( v );
}
inline constexpr units::quantity<double, units::money_in_cent_tag> operator"" _USD(
const long double v )
{
return units::from_usd( v );
}
inline constexpr units::money operator"" _kUSD( const unsigned long long v )
{
return units::from_kusd( v );
}
inline constexpr units::quantity<double, units::money_in_cent_tag> operator"" _kUSD(
const long double v )
{
return units::from_kusd( v );
}
#endif // CATA_SRC_UNITS_MONEY_H