| | | 1 | | using System.Globalization; |
| | | 2 | | |
| | | 3 | | namespace NLightning.Domain.Money; |
| | | 4 | | |
| | | 5 | | using Enums; |
| | | 6 | | |
| | | 7 | | public class LightningMoney |
| | | 8 | | { |
| | | 9 | | // For decimal.TryParse. None of the NumberStyles' composed values is useful for bitcoin style |
| | | 10 | | private const NumberStyles BitcoinStyle = NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite |
| | | 11 | | | NumberStyles.AllowDecimalPoint; |
| | | 12 | | |
| | | 13 | | private ulong _milliSatoshi; |
| | | 14 | | |
| | | 15 | | public const ulong Coin = 100 * 1000 * 1000 * 1000UL; |
| | | 16 | | public const ulong Cent = Coin / 100; |
| | | 17 | | public const ulong Nano = Cent / 100; |
| | | 18 | | |
| | | 19 | | public ulong MilliSatoshi |
| | | 20 | | { |
| | 1136 | 21 | | get => _milliSatoshi; |
| | | 22 | | set |
| | | 23 | | { |
| | 5228 | 24 | | _milliSatoshi = value; |
| | 5228 | 25 | | } |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | public long Satoshi |
| | | 29 | | { |
| | | 30 | | // Should round up to the nearest Satoshi |
| | 4380 | 31 | | get => checked((long)Math.Round(_milliSatoshi / 1_000D, MidpointRounding.ToNegativeInfinity)); |
| | | 32 | | set |
| | | 33 | | { |
| | 16 | 34 | | if (value < 0) |
| | 4 | 35 | | throw new ArgumentOutOfRangeException(nameof(value), "Satoshi value cannot be negative"); |
| | | 36 | | |
| | | 37 | | checked |
| | | 38 | | { |
| | 12 | 39 | | _milliSatoshi = (ulong)(value * 1000); |
| | | 40 | | } |
| | 12 | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | 748 | 44 | | public static LightningMoney Zero => 0UL; |
| | 404 | 45 | | public bool IsZero => _milliSatoshi == 0; |
| | | 46 | | |
| | | 47 | | #region Constructors |
| | | 48 | | |
| | 1964 | 49 | | public LightningMoney(ulong milliSatoshi) |
| | | 50 | | { |
| | 1964 | 51 | | MilliSatoshi = milliSatoshi; |
| | 1964 | 52 | | } |
| | | 53 | | |
| | 116 | 54 | | public LightningMoney(decimal amount, LightningMoneyUnit unit) |
| | | 55 | | { |
| | | 56 | | // sanity check. Only valid units are allowed |
| | 116 | 57 | | CheckLightningMoneyUnit(unit, nameof(unit)); |
| | | 58 | | checked |
| | | 59 | | { |
| | 112 | 60 | | var milliSats = amount * (long)unit; |
| | 112 | 61 | | MilliSatoshi = (ulong)milliSats; |
| | | 62 | | } |
| | 108 | 63 | | } |
| | | 64 | | |
| | 44 | 65 | | public LightningMoney(long amount, LightningMoneyUnit unit) |
| | | 66 | | { |
| | | 67 | | // Sanity check. Only valid units are allowed |
| | 44 | 68 | | CheckLightningMoneyUnit(unit, nameof(unit)); |
| | | 69 | | checked |
| | | 70 | | { |
| | 44 | 71 | | var milliSats = amount * (long)unit; |
| | 44 | 72 | | MilliSatoshi = (ulong)milliSats; |
| | | 73 | | } |
| | 44 | 74 | | } |
| | | 75 | | |
| | 3112 | 76 | | public LightningMoney(ulong amount, LightningMoneyUnit unit) |
| | | 77 | | { |
| | | 78 | | // Sanity check. Only valid units are allowed |
| | 3112 | 79 | | CheckLightningMoneyUnit(unit, nameof(unit)); |
| | | 80 | | checked |
| | | 81 | | { |
| | 3112 | 82 | | var milliSats = amount * (ulong)unit; |
| | 3112 | 83 | | MilliSatoshi = milliSats; |
| | | 84 | | } |
| | 3112 | 85 | | } |
| | | 86 | | |
| | | 87 | | #endregion |
| | | 88 | | |
| | | 89 | | #region Parsers |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Parse a bitcoin amount (Culture Invariant) |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="bitcoin"></param> |
| | | 95 | | /// <param name="nRet"></param> |
| | | 96 | | /// <returns></returns> |
| | | 97 | | public static bool TryParse(string bitcoin, out LightningMoney? nRet) |
| | | 98 | | { |
| | 16 | 99 | | nRet = null; |
| | | 100 | | |
| | 16 | 101 | | if (!decimal.TryParse(bitcoin, BitcoinStyle, CultureInfo.InvariantCulture, out var value)) |
| | | 102 | | { |
| | 8 | 103 | | return false; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | try |
| | | 107 | | { |
| | 8 | 108 | | nRet = new LightningMoney(value, LightningMoneyUnit.Btc); |
| | 8 | 109 | | return true; |
| | | 110 | | } |
| | 0 | 111 | | catch (OverflowException) |
| | | 112 | | { |
| | 0 | 113 | | return false; |
| | | 114 | | } |
| | 8 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Parse a bitcoin amount (Culture Invariant) |
| | | 119 | | /// </summary> |
| | | 120 | | /// <param name="bitcoin"></param> |
| | | 121 | | /// <returns></returns> |
| | | 122 | | public static LightningMoney? Parse(string bitcoin) |
| | | 123 | | { |
| | 8 | 124 | | if (TryParse(bitcoin, out var result)) |
| | | 125 | | { |
| | 4 | 126 | | return result; |
| | | 127 | | } |
| | | 128 | | |
| | 4 | 129 | | throw new FormatException("Impossible to parse the string in a bitcoin amount"); |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | #endregion |
| | | 133 | | |
| | | 134 | | #region Conversions |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Convert Money to decimal (same as ToDecimal) |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="unit"></param> |
| | | 140 | | /// <returns></returns> |
| | | 141 | | public decimal ToUnit(LightningMoneyUnit unit) |
| | | 142 | | { |
| | 184 | 143 | | CheckLightningMoneyUnit(unit, nameof(unit)); |
| | | 144 | | // overflow safe because (long / int) always fit in decimal |
| | | 145 | | // decimal operations are checked by default |
| | 184 | 146 | | return (decimal)MilliSatoshi / (ulong)unit; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Convert Money to decimal (same as ToUnit) |
| | | 151 | | /// </summary> |
| | | 152 | | /// <param name="unit"></param> |
| | | 153 | | /// <returns></returns> |
| | | 154 | | public decimal ToDecimal(LightningMoneyUnit unit) |
| | | 155 | | { |
| | 8 | 156 | | return ToUnit(unit); |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | #endregion |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Split the Money in parts without loss |
| | | 163 | | /// </summary> |
| | | 164 | | /// <param name="parts">The number of parts (must be more than 0)</param> |
| | | 165 | | /// <returns>The split money</returns> |
| | | 166 | | public IEnumerable<LightningMoney> Split(int parts) |
| | | 167 | | { |
| | 4 | 168 | | if (parts <= 0) |
| | 0 | 169 | | throw new ArgumentOutOfRangeException(nameof(parts), "Parts should be more than 0"); |
| | | 170 | | |
| | 4 | 171 | | var result = DivRem(_milliSatoshi, (ulong)parts, out var remain); |
| | | 172 | | |
| | 32 | 173 | | for (var i = 0; i < parts; i++) |
| | | 174 | | { |
| | 12 | 175 | | yield return MilliSatoshis(result + (remain > 0 ? 1UL : 0UL)); |
| | 12 | 176 | | if (remain > 0) |
| | | 177 | | { |
| | 4 | 178 | | remain--; |
| | | 179 | | } |
| | | 180 | | } |
| | 4 | 181 | | } |
| | | 182 | | |
| | | 183 | | #region Static Converters |
| | | 184 | | |
| | | 185 | | public static LightningMoney FromUnit(decimal amount, LightningMoneyUnit unit) |
| | | 186 | | { |
| | 80 | 187 | | return new LightningMoney(amount, unit); |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | public static LightningMoney Coins(decimal coins) |
| | | 191 | | { |
| | | 192 | | // overflow safe. |
| | | 193 | | // decimal operations are checked by default |
| | 4 | 194 | | return new LightningMoney(coins * Coin, LightningMoneyUnit.MilliSatoshi); |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | public static LightningMoney Bits(decimal bits) |
| | | 198 | | { |
| | | 199 | | // overflow safe. |
| | | 200 | | // decimal operations are checked by default |
| | 4 | 201 | | return new LightningMoney(bits * Cent, LightningMoneyUnit.MilliSatoshi); |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | public static LightningMoney Cents(decimal cents) |
| | | 205 | | { |
| | | 206 | | // overflow safe. |
| | | 207 | | // decimal operations are checked by default |
| | 4 | 208 | | return new LightningMoney(cents * Cent, LightningMoneyUnit.MilliSatoshi); |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | public static LightningMoney Satoshis(decimal sats) |
| | | 212 | | { |
| | 4 | 213 | | return new LightningMoney(sats, LightningMoneyUnit.Satoshi); |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | public static LightningMoney Satoshis(ulong sats) |
| | | 217 | | { |
| | 48 | 218 | | return new LightningMoney(sats, LightningMoneyUnit.Satoshi); |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | public static LightningMoney Satoshis(long sats) |
| | | 222 | | { |
| | 3060 | 223 | | return new LightningMoney((ulong)sats, LightningMoneyUnit.Satoshi); |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | public static LightningMoney MilliSatoshis(ulong milliSats) |
| | | 227 | | { |
| | 16 | 228 | | return new LightningMoney(milliSats); |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | public static LightningMoney MilliSatoshis(long sats) |
| | | 232 | | { |
| | 276 | 233 | | return new LightningMoney((ulong)sats); |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | #endregion |
| | | 237 | | |
| | | 238 | | #region IEquatable<Money> Members |
| | | 239 | | |
| | | 240 | | public bool Equals(LightningMoney? other) |
| | | 241 | | { |
| | 24 | 242 | | return other is not null && _milliSatoshi.Equals(other._milliSatoshi); |
| | | 243 | | } |
| | | 244 | | |
| | | 245 | | public int CompareTo(LightningMoney? other) |
| | | 246 | | { |
| | 0 | 247 | | return other is null ? 1 : _milliSatoshi.CompareTo(other._milliSatoshi); |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | #endregion |
| | | 251 | | |
| | | 252 | | #region IComparable Members |
| | | 253 | | |
| | | 254 | | public int CompareTo(object? obj) |
| | | 255 | | { |
| | 0 | 256 | | return obj switch |
| | 0 | 257 | | { |
| | 0 | 258 | | null => 1, |
| | 0 | 259 | | LightningMoney m => _milliSatoshi.CompareTo(m._milliSatoshi), |
| | 0 | 260 | | _ => _milliSatoshi.CompareTo((ulong)obj) |
| | 0 | 261 | | }; |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | #endregion |
| | | 265 | | |
| | | 266 | | #region Unary Operators |
| | | 267 | | |
| | | 268 | | public static LightningMoney operator -(LightningMoney left, LightningMoney right) |
| | | 269 | | { |
| | 304 | 270 | | ArgumentNullException.ThrowIfNull(left); |
| | 304 | 271 | | ArgumentNullException.ThrowIfNull(right); |
| | | 272 | | |
| | 304 | 273 | | if (left._milliSatoshi < right._milliSatoshi) |
| | 0 | 274 | | throw new ArithmeticException("LightningMoney does not support negative values"); |
| | | 275 | | |
| | 304 | 276 | | return new LightningMoney(checked(left._milliSatoshi - right._milliSatoshi)); |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | public static LightningMoney operator -(LightningMoney _) |
| | | 280 | | { |
| | 0 | 281 | | throw new ArithmeticException("LightningMoney does not support unary negation"); |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | public static LightningMoney operator +(LightningMoney left, LightningMoney right) |
| | | 285 | | { |
| | 4 | 286 | | ArgumentNullException.ThrowIfNull(left); |
| | 4 | 287 | | ArgumentNullException.ThrowIfNull(right); |
| | | 288 | | |
| | 4 | 289 | | return new LightningMoney(checked(left._milliSatoshi + right._milliSatoshi)); |
| | | 290 | | } |
| | | 291 | | |
| | | 292 | | public static LightningMoney operator *(ulong left, LightningMoney right) |
| | | 293 | | { |
| | 0 | 294 | | ArgumentNullException.ThrowIfNull(right); |
| | | 295 | | |
| | 0 | 296 | | return MilliSatoshis(checked(left * right._milliSatoshi)); |
| | | 297 | | } |
| | | 298 | | |
| | | 299 | | public static LightningMoney operator *(LightningMoney left, ulong right) |
| | | 300 | | { |
| | 0 | 301 | | ArgumentNullException.ThrowIfNull(left); |
| | | 302 | | |
| | 0 | 303 | | return MilliSatoshis(checked(left._milliSatoshi * right)); |
| | | 304 | | } |
| | | 305 | | |
| | | 306 | | public static LightningMoney operator *(long left, LightningMoney right) |
| | | 307 | | { |
| | 0 | 308 | | ArgumentNullException.ThrowIfNull(right); |
| | | 309 | | |
| | 0 | 310 | | return MilliSatoshis(checked((ulong)left * right._milliSatoshi)); |
| | | 311 | | } |
| | | 312 | | |
| | | 313 | | public static LightningMoney operator *(LightningMoney left, long right) |
| | | 314 | | { |
| | 0 | 315 | | ArgumentNullException.ThrowIfNull(left); |
| | | 316 | | |
| | 0 | 317 | | return MilliSatoshis(checked((ulong)right * left._milliSatoshi)); |
| | | 318 | | } |
| | | 319 | | |
| | | 320 | | public static LightningMoney operator *(decimal left, LightningMoney right) |
| | | 321 | | { |
| | 0 | 322 | | ArgumentNullException.ThrowIfNull(right); |
| | | 323 | | |
| | 0 | 324 | | return MilliSatoshis((ulong)(left * right._milliSatoshi)); |
| | | 325 | | } |
| | | 326 | | |
| | | 327 | | public static LightningMoney operator *(LightningMoney left, decimal right) |
| | | 328 | | { |
| | 0 | 329 | | ArgumentNullException.ThrowIfNull(left); |
| | | 330 | | |
| | 0 | 331 | | return MilliSatoshis((ulong)(right * left._milliSatoshi)); |
| | | 332 | | } |
| | | 333 | | |
| | | 334 | | public static LightningMoney operator /(LightningMoney left, ulong right) |
| | | 335 | | { |
| | 0 | 336 | | ArgumentNullException.ThrowIfNull(left); |
| | | 337 | | |
| | 0 | 338 | | return new LightningMoney(left._milliSatoshi / right); |
| | | 339 | | } |
| | | 340 | | |
| | | 341 | | public static bool operator <(LightningMoney left, LightningMoney right) |
| | | 342 | | { |
| | 4 | 343 | | ArgumentNullException.ThrowIfNull(left); |
| | 4 | 344 | | ArgumentNullException.ThrowIfNull(right); |
| | | 345 | | |
| | 4 | 346 | | return left._milliSatoshi < right._milliSatoshi; |
| | | 347 | | } |
| | | 348 | | |
| | | 349 | | public static bool operator >(LightningMoney left, LightningMoney right) |
| | | 350 | | { |
| | 296 | 351 | | ArgumentNullException.ThrowIfNull(left); |
| | 296 | 352 | | ArgumentNullException.ThrowIfNull(right); |
| | | 353 | | |
| | 296 | 354 | | return left._milliSatoshi > right._milliSatoshi; |
| | | 355 | | } |
| | | 356 | | |
| | | 357 | | public static bool operator <=(LightningMoney left, LightningMoney right) |
| | | 358 | | { |
| | 16 | 359 | | ArgumentNullException.ThrowIfNull(left); |
| | 16 | 360 | | ArgumentNullException.ThrowIfNull(right); |
| | | 361 | | |
| | 16 | 362 | | return left._milliSatoshi <= right._milliSatoshi; |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | public static bool operator >=(LightningMoney left, LightningMoney right) |
| | | 366 | | { |
| | 8 | 367 | | ArgumentNullException.ThrowIfNull(left); |
| | 8 | 368 | | ArgumentNullException.ThrowIfNull(right); |
| | | 369 | | |
| | 8 | 370 | | return left._milliSatoshi >= right._milliSatoshi; |
| | | 371 | | } |
| | | 372 | | |
| | | 373 | | #endregion |
| | | 374 | | |
| | | 375 | | #region Implicit Operators |
| | | 376 | | |
| | | 377 | | public static implicit operator LightningMoney(long value) |
| | | 378 | | { |
| | 0 | 379 | | return new LightningMoney((ulong)value); |
| | | 380 | | } |
| | | 381 | | |
| | | 382 | | public static implicit operator LightningMoney(ulong value) |
| | | 383 | | { |
| | 1188 | 384 | | return new LightningMoney(value); |
| | | 385 | | } |
| | | 386 | | |
| | | 387 | | public static implicit operator LightningMoney(string value) |
| | | 388 | | { |
| | 0 | 389 | | return Parse(value) ?? |
| | 0 | 390 | | throw new ArgumentException("Cannot parse value into a valid LightningMoney", nameof(value)); |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | public static implicit operator long(LightningMoney value) |
| | | 394 | | { |
| | 0 | 395 | | return checked((long)value.MilliSatoshi); |
| | | 396 | | } |
| | | 397 | | |
| | | 398 | | public static implicit operator ulong(LightningMoney value) |
| | | 399 | | { |
| | 100 | 400 | | return value.MilliSatoshi; |
| | | 401 | | } |
| | | 402 | | |
| | | 403 | | #endregion |
| | | 404 | | |
| | | 405 | | #region Equality Operators |
| | | 406 | | |
| | | 407 | | public override bool Equals(object? obj) |
| | | 408 | | { |
| | 120 | 409 | | return obj is LightningMoney item && _milliSatoshi.Equals(item._milliSatoshi); |
| | | 410 | | } |
| | | 411 | | |
| | | 412 | | public override int GetHashCode() |
| | | 413 | | { |
| | 0 | 414 | | return _milliSatoshi.GetHashCode(); |
| | | 415 | | } |
| | | 416 | | |
| | | 417 | | public static bool operator ==(LightningMoney? a, LightningMoney? b) |
| | | 418 | | { |
| | 8 | 419 | | if (ReferenceEquals(a, b)) |
| | 0 | 420 | | return true; |
| | 8 | 421 | | if (a is null || b is null) |
| | 0 | 422 | | return false; |
| | 8 | 423 | | return a._milliSatoshi == b._milliSatoshi; |
| | | 424 | | } |
| | | 425 | | |
| | | 426 | | public static bool operator !=(LightningMoney a, LightningMoney b) |
| | | 427 | | { |
| | 4 | 428 | | return !(a == b); |
| | | 429 | | } |
| | | 430 | | |
| | | 431 | | #endregion |
| | | 432 | | |
| | | 433 | | #region ToString |
| | | 434 | | |
| | | 435 | | /// <summary> |
| | | 436 | | /// Returns a culture invariant string representation of Bitcoin amount |
| | | 437 | | /// </summary> |
| | | 438 | | /// <returns></returns> |
| | | 439 | | public override string ToString() |
| | | 440 | | { |
| | 0 | 441 | | return ToString(false); |
| | | 442 | | } |
| | | 443 | | |
| | | 444 | | /// <summary> |
| | | 445 | | /// Returns a culture invariant string representation of Bitcoin amount |
| | | 446 | | /// </summary> |
| | | 447 | | /// <param name="trimExcessZero">True if trim excess zeros</param> |
| | | 448 | | /// <returns></returns> |
| | | 449 | | public string ToString(bool trimExcessZero = true) |
| | | 450 | | { |
| | 12 | 451 | | var val = (decimal)_milliSatoshi / (ulong)LightningMoneyUnit.Btc; |
| | 12 | 452 | | var decPos = trimExcessZero ? 2 : 11; |
| | 12 | 453 | | var zeros = new string('0', decPos); |
| | 12 | 454 | | var rest = new string('#', 11 - decPos); |
| | 12 | 455 | | var fmt = "{0:0" + ("." + zeros + rest) + "}"; |
| | | 456 | | |
| | 12 | 457 | | return string.Format(CultureInfo.InvariantCulture, fmt, val); |
| | | 458 | | } |
| | | 459 | | |
| | | 460 | | #endregion |
| | | 461 | | |
| | | 462 | | /// <summary> |
| | | 463 | | /// Tell if the amount is almost equal to this instance |
| | | 464 | | /// </summary> |
| | | 465 | | /// <param name="amount"></param> |
| | | 466 | | /// <param name="dust">more or less amount</param> |
| | | 467 | | /// <returns>true if equals, else false</returns> |
| | | 468 | | public bool Almost(LightningMoney amount, LightningMoney dust) |
| | | 469 | | { |
| | 8 | 470 | | ArgumentNullException.ThrowIfNull(amount); |
| | 8 | 471 | | ArgumentNullException.ThrowIfNull(dust); |
| | | 472 | | |
| | 8 | 473 | | return amount - this <= dust; |
| | | 474 | | } |
| | | 475 | | |
| | | 476 | | /// <summary> |
| | | 477 | | /// Tell if the amount is almost equal to this instance |
| | | 478 | | /// </summary> |
| | | 479 | | /// <param name="amount"></param> |
| | | 480 | | /// <param name="margin">error margin (between 0 and 1)</param> |
| | | 481 | | /// <returns>true if equals, else false</returns> |
| | | 482 | | public bool Almost(LightningMoney amount, decimal margin) |
| | | 483 | | { |
| | 4 | 484 | | ArgumentNullException.ThrowIfNull(amount); |
| | 4 | 485 | | if (margin is < 0.0m or > 1.0m) |
| | 0 | 486 | | throw new ArgumentOutOfRangeException(nameof(margin), "margin should be between 0 and 1"); |
| | | 487 | | |
| | 4 | 488 | | var dust = new LightningMoney(MilliSatoshi * margin, LightningMoneyUnit.MilliSatoshi); |
| | 4 | 489 | | return Almost(amount, dust); |
| | | 490 | | } |
| | | 491 | | |
| | | 492 | | public static LightningMoney Min(LightningMoney a, LightningMoney b) |
| | | 493 | | { |
| | 4 | 494 | | ArgumentNullException.ThrowIfNull(a); |
| | 4 | 495 | | ArgumentNullException.ThrowIfNull(b); |
| | | 496 | | |
| | 4 | 497 | | return a <= b ? a : b; |
| | | 498 | | } |
| | | 499 | | |
| | | 500 | | public static LightningMoney Max(LightningMoney a, LightningMoney b) |
| | | 501 | | { |
| | 4 | 502 | | ArgumentNullException.ThrowIfNull(a); |
| | 4 | 503 | | ArgumentNullException.ThrowIfNull(b); |
| | | 504 | | |
| | 4 | 505 | | return a >= b ? a : b; |
| | | 506 | | } |
| | | 507 | | |
| | | 508 | | #region IMoney Members |
| | | 509 | | |
| | | 510 | | public LightningMoney Add(LightningMoney money) |
| | | 511 | | { |
| | 0 | 512 | | return this + money; |
| | | 513 | | } |
| | | 514 | | |
| | | 515 | | public LightningMoney Sub(LightningMoney money) |
| | | 516 | | { |
| | 0 | 517 | | return this - money; |
| | | 518 | | } |
| | | 519 | | |
| | | 520 | | public LightningMoney Negate() |
| | | 521 | | { |
| | 4 | 522 | | throw new ArithmeticException("LightningMoney does not support unary negation"); |
| | | 523 | | } |
| | | 524 | | |
| | | 525 | | #endregion |
| | | 526 | | |
| | | 527 | | private static void CheckLightningMoneyUnit(LightningMoneyUnit value, string paramName) |
| | | 528 | | { |
| | 3456 | 529 | | var typeOfMoneyUnit = typeof(LightningMoneyUnit); |
| | 3456 | 530 | | if (!Enum.IsDefined(typeOfMoneyUnit, value)) |
| | | 531 | | { |
| | 4 | 532 | | throw new ArgumentException("Invalid value for LightningMoneyUnit", paramName); |
| | | 533 | | } |
| | 3452 | 534 | | } |
| | | 535 | | |
| | | 536 | | private static ulong DivRem(ulong a, ulong b, out ulong result) |
| | | 537 | | { |
| | 4 | 538 | | result = a % b; |
| | 4 | 539 | | return a / b; |
| | | 540 | | } |
| | | 541 | | } |