-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMoney.cs
More file actions
320 lines (303 loc) · 12 KB
/
Money.cs
File metadata and controls
320 lines (303 loc) · 12 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using BytecodeApi.Extensions;
using System.Diagnostics;
namespace BytecodeApi.Data;
/// <summary>
/// Represents a money value composed of an amount and a currency.
/// </summary>
[DebuggerDisplay($"{nameof(Money)}: Amount = {{Amount}}, Currency = {{Currency}}")]
public readonly struct Money : IComparable, IComparable<Money>, IEquatable<Money>
{
/// <summary>
/// Gets the amount of this money value.
/// </summary>
public decimal Amount { get; init; }
/// <summary>
/// Gets the currency of this money value.
/// </summary>
public Currency Currency { get; init; }
/// <summary>
/// Gets the currency symbol. If the currency has no symbol, returns the currency code.
/// </summary>
public string CurrencySymbol => Currency.GetDescription() ?? Currency.ToString();
/// <summary>
/// Initializes a new instance of the <see cref="Money" /> structure with the specified amount and the <see cref="Currency.Unknown" /> currency.
/// </summary>
/// <param name="amount">The amount of this money value.</param>
public Money(decimal amount)
{
Amount = amount;
Currency = Currency.Unknown;
}
/// <summary>
/// Initializes a new instance of the <see cref="Money" /> structure with the specified amount and currency.
/// </summary>
/// <param name="amount">The amount of this money value.</param>
/// <param name="currency">The currency of this money value.</param>
public Money(decimal amount, Currency currency)
{
Amount = amount;
Currency = currency;
}
/// <summary>
/// Returns a formatted <see cref="string" /> of this <see cref="Money" /> value.
/// </summary>
/// <returns>
/// A formatted <see cref="string" /> representing this instance.
/// </returns>
public string Format()
{
return Format(2, true);
}
/// <summary>
/// Returns a formatted <see cref="string" /> of this <see cref="Money" /> value using the specified formatting parameters.
/// </summary>
/// <param name="decimals">The number of decimals to round the result to. The default value is 2.</param>
/// <param name="thousandsSeparator"><see langword="true" /> to use a thousands separator.</param>
/// <returns>
/// A formatted <see cref="string" /> representing this instance.
/// </returns>
public string Format(int decimals, bool thousandsSeparator)
{
return Format(decimals, thousandsSeparator, true);
}
/// <summary>
/// Returns a formatted <see cref="string" /> of this <see cref="Money" /> value using the specified formatting parameters.
/// </summary>
/// <param name="decimals">The number of decimals to round the result to. The default value is 2.</param>
/// <param name="thousandsSeparator"><see langword="true" /> to use a thousands separator.</param>
/// <param name="includeCurrency"><see langword="true" /> to append the currency.</param>
/// <returns>
/// A formatted <see cref="string" /> representing this instance.
/// </returns>
public string Format(int decimals, bool thousandsSeparator, bool includeCurrency)
{
Check.ArgumentOutOfRangeEx.GreaterEqual0(decimals);
Check.ArgumentOutOfRange(decimals <= 15, nameof(decimals), "The number of decimals must be in range of 0...15.");
string format = Math
.Round(Amount, decimals)
.ToStringInvariant((thousandsSeparator ? "#,#" : "#") + (decimals > 0 ? "." + '0'.Repeat(decimals) : null))
.Swap('.', ',');
if (includeCurrency)
{
format += " " + Currency;
}
return format;
}
/// <summary>
/// Compares this instance to a specified <see cref="Money" /> and returns a comparison of their relative values.
/// Both values must have matching currencies.
/// </summary>
/// <param name="obj">An <see cref="object" /> to compare with this instance.</param>
/// <returns>
/// A value that indicates the relative order of the objects being compared.
/// </returns>
public int CompareTo(object? obj)
{
Check.Argument(obj is Money, nameof(obj), nameof(obj) + " is not the same type as this instance.");
return CompareTo((Money)obj!);
}
/// <summary>
/// Compares this instance to a specified <see cref="Money" /> and returns a comparison of their relative values.
/// Both values must have matching currencies.
/// </summary>
/// <param name="other">A <see cref="Money" /> to compare with this instance.</param>
/// <returns>
/// A value that indicates the relative order of the objects being compared.
/// </returns>
public int CompareTo(Money other)
{
Check.Argument(Currency == other.Currency, nameof(other), "Currency of money values must match.");
return Amount.CompareTo(other.Amount);
}
/// <summary>
/// Returns a <see cref="string" /> that represents this instance.
/// <para>Example: 12.34 USD</para>
/// </summary>
/// <returns>
/// A <see cref="string" /> that represents this instance.
/// </returns>
public override string ToString()
{
return $"{Amount} {Currency}";
}
/// <summary>
/// Determines whether the specified <see cref="object" /> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="object" /> to compare with this instance.</param>
/// <returns>
/// <see langword="true" />, if the specified <see cref="object" /> is equal to this instance;
/// otherwise, <see langword="false" />.
/// </returns>
public override bool Equals([NotNullWhen(true)] object? obj)
{
return obj is Money money && Equals(money);
}
/// <summary>
/// Determines whether this instance is equal to another <see cref="Money" />.
/// </summary>
/// <param name="other">The <see cref="Money" /> to compare to this instance.</param>
/// <returns>
/// <see langword="true" />, if this instance is equal to the <paramref name="other" /> parameter;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Equals(Money other)
{
return Amount == other.Amount && Currency == other.Currency;
}
/// <summary>
/// Returns a hash code for this <see cref="Money" />.
/// </summary>
/// <returns>
/// The hash code for this <see cref="Money" /> instance.
/// </returns>
public override int GetHashCode()
{
return HashCode.Combine(Amount, Currency);
}
/// <summary>
/// Compares two <see cref="Money" /> values for equality.
/// </summary>
/// <param name="a">The first <see cref="Money" /> to compare.</param>
/// <param name="b">The second <see cref="Money" /> to compare.</param>
/// <returns>
/// <see langword="true" />, if both <see cref="Money" /> values are equal;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool operator ==(Money a, Money b)
{
return Equals(a, b);
}
/// <summary>
/// Compares two <see cref="Money" /> values for inequality.
/// </summary>
/// <param name="a">The first <see cref="Money" /> to compare.</param>
/// <param name="b">The second <see cref="Money" /> to compare.</param>
/// <returns>
/// <see langword="true" />, if both <see cref="Money" /> values are not equal;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool operator !=(Money a, Money b)
{
return !Equals(a, b);
}
/// <summary>
/// Returns a value indicating whether a specified <see cref="Money" /> value is less than another specified <see cref="Money" /> value.
/// Both values must have matching currencies.
/// </summary>
/// <param name="a">The first value to compare.</param>
/// <param name="b">The second value to compare.</param>
/// <returns>
/// <see langword="true" />, if <paramref name="a" /> is less than <paramref name="b" />;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool operator <(Money a, Money b)
{
Check.Argument(a.Currency == b.Currency, null, "Currency of money values must match.");
return a.Amount < b.Amount;
}
/// <summary>
/// Returns a value indicating whether a specified <see cref="Money" /> value is less than or equal to another specified <see cref="Money" /> value.
/// Both values must have matching currencies.
/// </summary>
/// <param name="a">The first value to compare.</param>
/// <param name="b">The second value to compare.</param>
/// <returns>
/// <see langword="true" />, if <paramref name="a" /> is less than or equal to <paramref name="b" />;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool operator <=(Money a, Money b)
{
Check.Argument(a.Currency == b.Currency, null, "Currency of money values must match.");
return a.Amount <= b.Amount;
}
/// <summary>
/// Returns a value indicating whether a specified <see cref="Money" /> value is greater than another specified <see cref="Money" /> value.
/// Both values must have matching currencies.
/// </summary>
/// <param name="a">The first value to compare.</param>
/// <param name="b">The second value to compare.</param>
/// <returns>
/// <see langword="true" />, if <paramref name="a" /> is greater than <paramref name="b" />;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool operator >(Money a, Money b)
{
Check.Argument(a.Currency == b.Currency, null, "Currency of money values must match.");
return a.Amount > b.Amount;
}
/// <summary>
/// Returns a value indicating whether a specified <see cref="Money" /> value is greater than or equal to another specified <see cref="Money" /> value.
/// Both values must have matching currencies.
/// </summary>
/// <param name="a">The first value to compare.</param>
/// <param name="b">The second value to compare.</param>
/// <returns>
/// <see langword="true" />, if <paramref name="a" /> is greater than or equal to <paramref name="b" />;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool operator >=(Money a, Money b)
{
Check.Argument(a.Currency == b.Currency, null, "Currency of money values must match.");
return a.Amount >= b.Amount;
}
/// <summary>
/// Adds two specified <see cref="Money" /> values.
/// Both values must have matching currencies.
/// </summary>
/// <param name="a">The first value to add.</param>
/// <param name="b">The second value to add.</param>
/// <returns>
/// The result of adding <paramref name="a" /> and <paramref name="b" />.
/// </returns>
public static Money operator +(Money a, Money b)
{
Check.Argument(a.Currency == b.Currency, null, "Currency of money values must match.");
return new(a.Amount + b.Amount, a.Currency);
}
/// <summary>
/// Subtracts two specified <see cref="Money" /> values.
/// Both values must have matching currencies.
/// </summary>
/// <param name="a">The minuend.</param>
/// <param name="b">The subtrahend.</param>
/// <returns>
/// The result of adding <paramref name="b" /> from <paramref name="a" />.
/// </returns>
public static Money operator -(Money a, Money b)
{
Check.Argument(a.Currency == b.Currency, null, "Currency of money values must match.");
return new(a.Amount - b.Amount, a.Currency);
}
/// <summary>
/// Multiplies a specified <see cref="Money" /> value and a specified <see cref="double" /> value.
/// </summary>
/// <param name="a">The first value to multiply.</param>
/// <param name="b">The second value to multiply.</param>
/// <returns>
/// The result of multiplying <paramref name="a" /> by <paramref name="b" />.
/// </returns>
public static Money operator *(Money a, double b)
{
return new(a.Amount * (decimal)b, a.Currency);
}
/// <summary>
/// Divides a specified <see cref="Money" /> value and a specified <see cref="double" /> value.
/// </summary>
/// <param name="a">The dividend.</param>
/// <param name="b">The divisor.</param>
/// <returns>
/// The result of dividing <paramref name="a" /> by <paramref name="b" />.
/// </returns>
public static Money operator /(Money a, double b)
{
return new(a.Amount / (decimal)b, a.Currency);
}
/// <summary>
/// Defines an explicit conversion of a <see cref="Money" /> to a <see cref="decimal" />.
/// </summary>
/// <param name="value">The <see cref="Money" /> to convert.</param>
public static explicit operator decimal(Money value)
{
return value.Amount;
}
}