public
Last active

BigDecimal type in .NET

  • Download Gist
BigDecimal.cs
C#
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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
using System;
using System.Linq;
 
namespace System.Numerics
{
public struct BigDecimal : IConvertible, IFormattable, IComparable, IComparable<BigDecimal>, IEquatable<BigDecimal>
{
public static readonly BigDecimal MinusOne = new BigDecimal(BigInteger.MinusOne, 0);
public static readonly BigDecimal Zero = new BigDecimal(BigInteger.Zero, 0);
public static readonly BigDecimal One = new BigDecimal(BigInteger.One, 0);
 
private readonly BigInteger _unscaledValue;
private readonly int _scale;
 
public BigDecimal(double value)
: this((decimal)value) { }
 
public BigDecimal(float value)
: this((decimal)value) { }
 
public BigDecimal(decimal value)
{
var bytes = FromDecimal(value);
 
var unscaledValueBytes = new byte[12];
Array.Copy(bytes, unscaledValueBytes, unscaledValueBytes.Length);
 
var unscaledValue = new BigInteger(unscaledValueBytes);
var scale = bytes[14];
 
if (bytes[15] == 128)
unscaledValue *= BigInteger.MinusOne;
 
_unscaledValue = unscaledValue;
_scale = scale;
}
 
public BigDecimal(int value)
: this(new BigInteger(value), 0) { }
 
public BigDecimal(long value)
: this(new BigInteger(value), 0) { }
 
public BigDecimal(uint value)
: this(new BigInteger(value), 0) { }
 
public BigDecimal(ulong value)
: this(new BigInteger(value), 0) { }
 
public BigDecimal(BigInteger unscaledValue, int scale)
{
_unscaledValue = unscaledValue;
_scale = scale;
}
 
public BigDecimal(byte[] value)
{
byte[] number = new byte[value.Length - 4];
byte[] flags = new byte[4];
 
Array.Copy(value, 0, number, 0, number.Length);
Array.Copy(value, value.Length - 4, flags, 0, 4);
 
_unscaledValue = new BigInteger(number);
_scale = BitConverter.ToInt32(flags, 0);
}
 
public bool IsEven { get { return _unscaledValue.IsEven; } }
public bool IsOne { get { return _unscaledValue.IsOne; } }
public bool IsPowerOfTwo { get { return _unscaledValue.IsPowerOfTwo; } }
public bool IsZero { get { return _unscaledValue.IsZero; } }
public int Sign { get { return _unscaledValue.Sign; } }
 
public override string ToString()
{
var number = _unscaledValue.ToString("G");
 
if (_scale > 0)
return number.Insert(number.Length - _scale, ".");
 
return number;
}
 
public byte[] ToByteArray()
{
var unscaledValue = _unscaledValue.ToByteArray();
var scale = BitConverter.GetBytes(_scale);
 
var bytes = new byte[unscaledValue.Length + scale.Length];
Array.Copy(unscaledValue, 0, bytes, 0, unscaledValue.Length);
Array.Copy(scale, 0, bytes, unscaledValue.Length, scale.Length);
 
return bytes;
}
 
private static byte[] FromDecimal(decimal d)
{
byte[] bytes = new byte[16];
 
int[] bits = decimal.GetBits(d);
int lo = bits[0];
int mid = bits[1];
int hi = bits[2];
int flags = bits[3];
 
bytes[0] = (byte)lo;
bytes[1] = (byte)(lo >> 8);
bytes[2] = (byte)(lo >> 0x10);
bytes[3] = (byte)(lo >> 0x18);
bytes[4] = (byte)mid;
bytes[5] = (byte)(mid >> 8);
bytes[6] = (byte)(mid >> 0x10);
bytes[7] = (byte)(mid >> 0x18);
bytes[8] = (byte)hi;
bytes[9] = (byte)(hi >> 8);
bytes[10] = (byte)(hi >> 0x10);
bytes[11] = (byte)(hi >> 0x18);
bytes[12] = (byte)flags;
bytes[13] = (byte)(flags >> 8);
bytes[14] = (byte)(flags >> 0x10);
bytes[15] = (byte)(flags >> 0x18);
 
return bytes;
}
 
#region Operators
 
public static bool operator ==(BigDecimal left, BigDecimal right)
{
return left.Equals(right);
}
 
public static bool operator !=(BigDecimal left, BigDecimal right)
{
return !left.Equals(right);
}
 
public static bool operator >(BigDecimal left, BigDecimal right)
{
return (left.CompareTo(right) > 0);
}
 
public static bool operator >=(BigDecimal left, BigDecimal right)
{
return (left.CompareTo(right) >= 0);
}
 
public static bool operator <(BigDecimal left, BigDecimal right)
{
return (left.CompareTo(right) < 0);
}
 
public static bool operator <=(BigDecimal left, BigDecimal right)
{
return (left.CompareTo(right) <= 0);
}
 
public static bool operator ==(BigDecimal left, decimal right)
{
return left.Equals(right);
}
 
public static bool operator !=(BigDecimal left, decimal right)
{
return !left.Equals(right);
}
 
public static bool operator >(BigDecimal left, decimal right)
{
return (left.CompareTo(right) > 0);
}
 
public static bool operator >=(BigDecimal left, decimal right)
{
return (left.CompareTo(right) >= 0);
}
 
public static bool operator <(BigDecimal left, decimal right)
{
return (left.CompareTo(right) < 0);
}
 
public static bool operator <=(BigDecimal left, decimal right)
{
return (left.CompareTo(right) <= 0);
}
 
public static bool operator ==(decimal left, BigDecimal right)
{
return left.Equals(right);
}
 
public static bool operator !=(decimal left, BigDecimal right)
{
return !left.Equals(right);
}
 
public static bool operator >(decimal left, BigDecimal right)
{
return (left.CompareTo(right) > 0);
}
 
public static bool operator >=(decimal left, BigDecimal right)
{
return (left.CompareTo(right) >= 0);
}
 
public static bool operator <(decimal left, BigDecimal right)
{
return (left.CompareTo(right) < 0);
}
 
public static bool operator <=(decimal left, BigDecimal right)
{
return (left.CompareTo(right) <= 0);
}
 
#endregion
 
#region Explicity and Implicit Casts
 
public static explicit operator byte(BigDecimal value) { return value.ToType<byte>(); }
public static explicit operator sbyte(BigDecimal value) { return value.ToType<sbyte>(); }
public static explicit operator short(BigDecimal value) { return value.ToType<short>(); }
public static explicit operator int(BigDecimal value) { return value.ToType<int>(); }
public static explicit operator long(BigDecimal value) { return value.ToType<long>(); }
public static explicit operator ushort(BigDecimal value) { return value.ToType<ushort>(); }
public static explicit operator uint(BigDecimal value) { return value.ToType<uint>(); }
public static explicit operator ulong(BigDecimal value) { return value.ToType<ulong>(); }
public static explicit operator float(BigDecimal value) { return value.ToType<float>(); }
public static explicit operator double(BigDecimal value) { return value.ToType<double>(); }
public static explicit operator decimal(BigDecimal value) { return value.ToType<decimal>(); }
public static explicit operator BigInteger(BigDecimal value)
{
var scaleDivisor = BigInteger.Pow(new BigInteger(10), value._scale);
var scaledValue = BigInteger.Divide(value._unscaledValue, scaleDivisor);
return scaledValue;
}
 
public static implicit operator BigDecimal(byte value) { return new BigDecimal(value); }
public static implicit operator BigDecimal(sbyte value) { return new BigDecimal(value); }
public static implicit operator BigDecimal(short value) { return new BigDecimal(value); }
public static implicit operator BigDecimal(int value) { return new BigDecimal(value); }
public static implicit operator BigDecimal(long value) { return new BigDecimal(value); }
public static implicit operator BigDecimal(ushort value) { return new BigDecimal(value); }
public static implicit operator BigDecimal(uint value) { return new BigDecimal(value); }
public static implicit operator BigDecimal(ulong value) { return new BigDecimal(value); }
public static implicit operator BigDecimal(float value) { return new BigDecimal(value); }
public static implicit operator BigDecimal(double value) { return new BigDecimal(value); }
public static implicit operator BigDecimal(decimal value) { return new BigDecimal(value); }
public static implicit operator BigDecimal(BigInteger value) { return new BigDecimal(value, 0); }
 
#endregion
 
public T ToType<T>() where T : struct
{
return (T)((IConvertible)this).ToType(typeof(T), null);
}
 
object IConvertible.ToType(Type conversionType, IFormatProvider provider)
{
var scaleDivisor = BigInteger.Pow(new BigInteger(10), this._scale);
var remainder = BigInteger.Remainder(this._unscaledValue, scaleDivisor);
var scaledValue = BigInteger.Divide(this._unscaledValue, scaleDivisor);
 
if (scaledValue > new BigInteger(Decimal.MaxValue))
throw new ArgumentOutOfRangeException("value", "The value " + this._unscaledValue + " cannot fit into " + conversionType.Name + ".");
 
var leftOfDecimal = (decimal)scaledValue;
var rightOfDecimal = ((decimal)remainder) / ((decimal)scaleDivisor);
 
var value = leftOfDecimal + rightOfDecimal;
return Convert.ChangeType(value, conversionType);
}
 
public override bool Equals(object obj)
{
return ((obj is BigDecimal) && Equals((BigDecimal)obj));
}
 
public override int GetHashCode()
{
return _unscaledValue.GetHashCode() ^ _scale.GetHashCode();
}
 
#region IConvertible Members
 
TypeCode IConvertible.GetTypeCode()
{
return TypeCode.Object;
}
 
bool IConvertible.ToBoolean(IFormatProvider provider)
{
return Convert.ToBoolean(this);
}
 
byte IConvertible.ToByte(IFormatProvider provider)
{
return Convert.ToByte(this);
}
 
char IConvertible.ToChar(IFormatProvider provider)
{
throw new InvalidCastException("Cannot cast BigDecimal to Char");
}
 
DateTime IConvertible.ToDateTime(IFormatProvider provider)
{
throw new InvalidCastException("Cannot cast BigDecimal to DateTime");
}
 
decimal IConvertible.ToDecimal(IFormatProvider provider)
{
return Convert.ToDecimal(this);
}
 
double IConvertible.ToDouble(IFormatProvider provider)
{
return Convert.ToDouble(this);
}
 
short IConvertible.ToInt16(IFormatProvider provider)
{
return Convert.ToInt16(this);
}
 
int IConvertible.ToInt32(IFormatProvider provider)
{
return Convert.ToInt32(this);
}
 
long IConvertible.ToInt64(IFormatProvider provider)
{
return Convert.ToInt64(this);
}
 
sbyte IConvertible.ToSByte(IFormatProvider provider)
{
return Convert.ToSByte(this);
}
 
float IConvertible.ToSingle(IFormatProvider provider)
{
return Convert.ToSingle(this);
}
 
string IConvertible.ToString(IFormatProvider provider)
{
return Convert.ToString(this);
}
 
ushort IConvertible.ToUInt16(IFormatProvider provider)
{
return Convert.ToUInt16(this);
}
 
uint IConvertible.ToUInt32(IFormatProvider provider)
{
return Convert.ToUInt32(this);
}
 
ulong IConvertible.ToUInt64(IFormatProvider provider)
{
return Convert.ToUInt64(this);
}
 
#endregion
 
#region IFormattable Members
 
public string ToString(string format, IFormatProvider formatProvider)
{
throw new NotImplementedException();
}
 
#endregion
 
#region IComparable Members
 
public int CompareTo(object obj)
{
if (obj == null)
return 1;
 
if (!(obj is BigDecimal))
throw new ArgumentException("Compare to object must be a BigDecimal", "obj");
 
return CompareTo((BigDecimal)obj);
}
 
#endregion
 
#region IComparable<BigDecimal> Members
 
public int CompareTo(BigDecimal other)
{
var unscaledValueCompare = this._unscaledValue.CompareTo(other._unscaledValue);
var scaleCompare = this._scale.CompareTo(other._scale);
 
// if both are the same value, return the value
if (unscaledValueCompare == scaleCompare)
return unscaledValueCompare;
 
// if the scales are both the same return unscaled value
if (scaleCompare == 0)
return unscaledValueCompare;
 
var scaledValue = BigInteger.Divide(this._unscaledValue, BigInteger.Pow(new BigInteger(10), this._scale));
var otherScaledValue = BigInteger.Divide(other._unscaledValue, BigInteger.Pow(new BigInteger(10), other._scale));
 
return scaledValue.CompareTo(otherScaledValue);
}
 
#endregion
 
#region IEquatable<BigDecimal> Members
 
public bool Equals(BigDecimal other)
{
return this._scale == other._scale && this._unscaledValue == other._unscaledValue;
}
 
#endregion
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.