Last active
January 26, 2016 12:14
-
-
Save asbjornu/c1f43647c9c2e3723a7a to your computer and use it in GitHub Desktop.
Get whole number and decimals from a System.Decimal in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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