Skip to content

Instantly share code, notes, and snippets.

@asbjornu
Last active January 26, 2016 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asbjornu/c1f43647c9c2e3723a7a to your computer and use it in GitHub Desktop.
Save asbjornu/c1f43647c9c2e3723a7a to your computer and use it in GitHub Desktop.
Get whole number and decimals from a System.Decimal in C#
var value = 123.8723498m;
var major = (int)Math.Truncate(value);
var bits = decimal.GetBits(value);
var decimalPart = bits[3];
var bytes = BitConverter.GetBytes(decimalPart);
var decimalLength = bytes[2];
var pow = (int)Math.Pow(10, decimalLength);
var minor = (int)((value - major) * pow);
var formatted = String.Format("{0}.{1}", major, minor);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment