Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SqlMoney.cs
Go to the documentation of this file.
4using System.Xml;
7
9
10[XmlSchemaProvider("GetXsdType")]
12{
13 private bool _fNotNull;
14
15 private long _value;
16
17 public static readonly SqlMoney Null = new SqlMoney(fNull: true);
18
19 public static readonly SqlMoney Zero = new SqlMoney(0);
20
21 public static readonly SqlMoney MinValue = new SqlMoney(long.MinValue, 0);
22
23 public static readonly SqlMoney MaxValue = new SqlMoney(long.MaxValue, 0);
24
25 public bool IsNull => !_fNotNull;
26
27 public decimal Value
28 {
29 get
30 {
31 if (_fNotNull)
32 {
33 return ToDecimal();
34 }
35 throw new SqlNullValueException();
36 }
37 }
38
39 private SqlMoney(bool fNull)
40 {
41 _fNotNull = false;
42 _value = 0L;
43 }
44
45 internal SqlMoney(long value, int ignored)
46 {
47 _value = value;
48 _fNotNull = true;
49 }
50
51 public SqlMoney(int value)
52 {
53 _value = (long)value * 10000L;
54 _fNotNull = true;
55 }
56
57 public SqlMoney(long value)
58 {
59 if (value < -922337203685477L || value > 922337203685477L)
60 {
62 }
63 _value = value * 10000;
64 _fNotNull = true;
65 }
66
67 public SqlMoney(decimal value)
68 {
70 sqlDecimal.AdjustScale(4 - sqlDecimal.Scale, fRound: true);
71 if (sqlDecimal._data3 != 0 || sqlDecimal._data4 != 0)
72 {
74 }
75 bool isPositive = sqlDecimal.IsPositive;
76 ulong num = sqlDecimal._data1 + ((ulong)sqlDecimal._data2 << 32);
77 if ((isPositive && num > long.MaxValue) || (!isPositive && num > 9223372036854775808uL))
78 {
80 }
81 _value = (long)(isPositive ? num : (0L - num));
82 _fNotNull = true;
83 }
84
85 public SqlMoney(double value)
86 : this(new decimal(value))
87 {
88 }
89
90 public decimal ToDecimal()
91 {
92 if (IsNull)
93 {
94 throw new SqlNullValueException();
95 }
96 bool isNegative = false;
97 long num = _value;
98 if (_value < 0)
99 {
100 isNegative = true;
101 num = -_value;
102 }
103 return new decimal((int)num, (int)(num >> 32), 0, isNegative, 4);
104 }
105
106 public long ToInt64()
107 {
108 if (IsNull)
109 {
110 throw new SqlNullValueException();
111 }
112 long num = _value / 1000;
113 bool flag = num >= 0;
114 long num2 = num % 10;
115 num /= 10;
116 if (num2 >= 5)
117 {
118 num = ((!flag) ? (num - 1) : (num + 1));
119 }
120 return num;
121 }
122
123 public int ToInt32()
124 {
125 return checked((int)ToInt64());
126 }
127
128 public double ToDouble()
129 {
130 return decimal.ToDouble(ToDecimal());
131 }
132
133 public static implicit operator SqlMoney(decimal x)
134 {
135 return new SqlMoney(x);
136 }
137
138 public static explicit operator SqlMoney(double x)
139 {
140 return new SqlMoney(x);
141 }
142
143 public static implicit operator SqlMoney(long x)
144 {
145 return new SqlMoney(new decimal(x));
146 }
147
148 public static explicit operator decimal(SqlMoney x)
149 {
150 return x.Value;
151 }
152
153 public override string ToString()
154 {
155 if (IsNull)
156 {
157 return SQLResource.NullString;
158 }
159 return ToDecimal().ToString("#0.00##", null);
160 }
161
162 public static SqlMoney Parse(string s)
163 {
164 if (s == SQLResource.NullString)
165 {
166 return Null;
167 }
168 decimal result;
169 return (!decimal.TryParse(s, NumberStyles.Integer | NumberStyles.AllowTrailingSign | NumberStyles.AllowParentheses | NumberStyles.AllowDecimalPoint | NumberStyles.AllowCurrencySymbol, NumberFormatInfo.InvariantInfo, out result)) ? new SqlMoney(decimal.Parse(s, NumberStyles.Currency, NumberFormatInfo.CurrentInfo)) : new SqlMoney(result);
170 }
171
172 public static SqlMoney operator -(SqlMoney x)
173 {
174 if (x.IsNull)
175 {
176 return Null;
177 }
178 if (x._value == -922337203685477L)
179 {
181 }
182 return new SqlMoney(-x._value, 0);
183 }
184
186 {
187 try
188 {
189 return (x.IsNull || y.IsNull) ? Null : new SqlMoney(checked(x._value + y._value), 0);
190 }
191 catch (OverflowException)
192 {
194 }
195 }
196
198 {
199 try
200 {
201 return (x.IsNull || y.IsNull) ? Null : new SqlMoney(checked(x._value - y._value), 0);
202 }
203 catch (OverflowException)
204 {
206 }
207 }
208
210 {
211 if (!x.IsNull && !y.IsNull)
212 {
213 return new SqlMoney(decimal.Multiply(x.ToDecimal(), y.ToDecimal()));
214 }
215 return Null;
216 }
217
219 {
220 if (!x.IsNull && !y.IsNull)
221 {
222 return new SqlMoney(decimal.Divide(x.ToDecimal(), y.ToDecimal()));
223 }
224 return Null;
225 }
226
227 public static explicit operator SqlMoney(SqlBoolean x)
228 {
229 if (!x.IsNull)
230 {
231 return new SqlMoney(x.ByteValue);
232 }
233 return Null;
234 }
235
236 public static implicit operator SqlMoney(SqlByte x)
237 {
238 if (!x.IsNull)
239 {
240 return new SqlMoney(x.Value);
241 }
242 return Null;
243 }
244
245 public static implicit operator SqlMoney(SqlInt16 x)
246 {
247 if (!x.IsNull)
248 {
249 return new SqlMoney(x.Value);
250 }
251 return Null;
252 }
253
254 public static implicit operator SqlMoney(SqlInt32 x)
255 {
256 if (!x.IsNull)
257 {
258 return new SqlMoney(x.Value);
259 }
260 return Null;
261 }
262
263 public static implicit operator SqlMoney(SqlInt64 x)
264 {
265 if (!x.IsNull)
266 {
267 return new SqlMoney(x.Value);
268 }
269 return Null;
270 }
271
272 public static explicit operator SqlMoney(SqlSingle x)
273 {
274 if (!x.IsNull)
275 {
276 return new SqlMoney(x.Value);
277 }
278 return Null;
279 }
280
281 public static explicit operator SqlMoney(SqlDouble x)
282 {
283 if (!x.IsNull)
284 {
285 return new SqlMoney(x.Value);
286 }
287 return Null;
288 }
289
290 public static explicit operator SqlMoney(SqlDecimal x)
291 {
292 if (!x.IsNull)
293 {
294 return new SqlMoney(x.Value);
295 }
296 return Null;
297 }
298
299 public static explicit operator SqlMoney(SqlString x)
300 {
301 if (!x.IsNull)
302 {
303 return new SqlMoney(decimal.Parse(x.Value, NumberStyles.Currency, null));
304 }
305 return Null;
306 }
307
309 {
310 if (!x.IsNull && !y.IsNull)
311 {
312 return new SqlBoolean(x._value == y._value);
313 }
314 return SqlBoolean.Null;
315 }
316
318 {
319 return !(x == y);
320 }
321
323 {
324 if (!x.IsNull && !y.IsNull)
325 {
326 return new SqlBoolean(x._value < y._value);
327 }
328 return SqlBoolean.Null;
329 }
330
332 {
333 if (!x.IsNull && !y.IsNull)
334 {
335 return new SqlBoolean(x._value > y._value);
336 }
337 return SqlBoolean.Null;
338 }
339
341 {
342 if (!x.IsNull && !y.IsNull)
343 {
344 return new SqlBoolean(x._value <= y._value);
345 }
346 return SqlBoolean.Null;
347 }
348
350 {
351 if (!x.IsNull && !y.IsNull)
352 {
353 return new SqlBoolean(x._value >= y._value);
354 }
355 return SqlBoolean.Null;
356 }
357
358 public static SqlMoney Add(SqlMoney x, SqlMoney y)
359 {
360 return x + y;
361 }
362
363 public static SqlMoney Subtract(SqlMoney x, SqlMoney y)
364 {
365 return x - y;
366 }
367
368 public static SqlMoney Multiply(SqlMoney x, SqlMoney y)
369 {
370 return x * y;
371 }
372
373 public static SqlMoney Divide(SqlMoney x, SqlMoney y)
374 {
375 return x / y;
376 }
377
378 public static SqlBoolean Equals(SqlMoney x, SqlMoney y)
379 {
380 return x == y;
381 }
382
384 {
385 return x != y;
386 }
387
389 {
390 return x < y;
391 }
392
394 {
395 return x > y;
396 }
397
399 {
400 return x <= y;
401 }
402
404 {
405 return x >= y;
406 }
407
409 {
410 return (SqlBoolean)this;
411 }
412
414 {
415 return (SqlByte)this;
416 }
417
419 {
420 return this;
421 }
422
424 {
425 return (SqlInt16)this;
426 }
427
429 {
430 return (SqlInt32)this;
431 }
432
434 {
435 return (SqlInt64)this;
436 }
437
439 {
440 return this;
441 }
442
444 {
445 return this;
446 }
447
449 {
450 return (SqlString)this;
451 }
452
453 public int CompareTo(object? value)
454 {
456 {
457 return CompareTo(value2);
458 }
459 throw ADP.WrongType(value.GetType(), typeof(SqlMoney));
460 }
461
463 {
464 if (IsNull)
465 {
466 if (!value.IsNull)
467 {
468 return -1;
469 }
470 return 0;
471 }
472 if (value.IsNull)
473 {
474 return 1;
475 }
476 if (this < value)
477 {
478 return -1;
479 }
480 if (this > value)
481 {
482 return 1;
483 }
484 return 0;
485 }
486
487 public override bool Equals([NotNullWhen(true)] object? value)
488 {
489 if (!(value is SqlMoney sqlMoney))
490 {
491 return false;
492 }
493 if (sqlMoney.IsNull || IsNull)
494 {
495 if (sqlMoney.IsNull)
496 {
497 return IsNull;
498 }
499 return false;
500 }
501 return (this == sqlMoney).Value;
502 }
503
504 public override int GetHashCode()
505 {
506 if (!IsNull)
507 {
508 return _value.GetHashCode();
509 }
510 return 0;
511 }
512
514 {
515 return null;
516 }
517
519 {
520 string attribute = reader.GetAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance");
521 if (attribute != null && XmlConvert.ToBoolean(attribute))
522 {
523 reader.ReadElementString();
524 _fNotNull = false;
525 }
526 else
527 {
529 _fNotNull = sqlMoney._fNotNull;
530 _value = sqlMoney._value;
531 }
532 }
533
535 {
536 if (IsNull)
537 {
538 writer.WriteAttributeString("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true");
539 }
540 else
541 {
542 writer.WriteString(XmlConvert.ToString(ToDecimal()));
543 }
544 }
545
547 {
548 return new XmlQualifiedName("decimal", "http://www.w3.org/2001/XMLSchema");
549 }
550}
static Exception WrongType(Type got, Type expected)
Definition ADP.cs:174
static bool ToBoolean(string s)
static string ToString(bool value)
static decimal ToDecimal(string s)
string? GetAttribute(string name)
virtual string ReadElementString()
Definition XmlReader.cs:667
static readonly SqlBoolean Null
Definition SqlBoolean.cs:21
override string ToString()
Definition SqlMoney.cs:153
static SqlMoney Divide(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:373
static SqlBoolean operator>=(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:349
static SqlBoolean Equals(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:378
static SqlMoney Subtract(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:363
static SqlBoolean operator>(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:331
static SqlBoolean LessThan(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:388
static SqlMoney Add(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:358
static readonly SqlMoney Zero
Definition SqlMoney.cs:19
static SqlBoolean GreaterThanOrEqual(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:403
static SqlMoney operator+(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:185
SqlMoney(long value, int ignored)
Definition SqlMoney.cs:45
int CompareTo(SqlMoney value)
Definition SqlMoney.cs:462
static SqlBoolean operator<=(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:340
static readonly SqlMoney MinValue
Definition SqlMoney.cs:21
static SqlBoolean GreaterThan(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:393
override bool Equals([NotNullWhen(true)] object? value)
Definition SqlMoney.cs:487
static SqlBoolean operator<(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:322
static SqlMoney Parse(string s)
Definition SqlMoney.cs:162
static SqlBoolean LessThanOrEqual(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:398
static readonly SqlMoney MaxValue
Definition SqlMoney.cs:23
static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet)
Definition SqlMoney.cs:546
static readonly SqlMoney Null
Definition SqlMoney.cs:17
static SqlBoolean NotEquals(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:383
static SqlBoolean operator==(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:308
static SqlMoney operator-(SqlMoney x)
Definition SqlMoney.cs:172
static SqlMoney operator*(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:209
static SqlMoney Multiply(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:368
int CompareTo(object? value)
Definition SqlMoney.cs:453
static SqlMoney operator/(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:218
static SqlBoolean operator!=(SqlMoney x, SqlMoney y)
Definition SqlMoney.cs:317