Working with Decimals
-
I have a decimal number with a variable amount of digits following the decimal. I need to make the number a whole number and in doing so I need to keep track of how many decimal places there were. ie change 28.9876 to 289874 and store there were 4 decimal places. I am currently doing this by converting to a string and checking how many are after the '.' This seems a poor way to solve this problem, but I am drawing a blank on other approaches. Can anyone offer other more efficient/elegant solutions?
this thing looks like it was written by an epileptic ferret Dave Kreskowiak
-
I have a decimal number with a variable amount of digits following the decimal. I need to make the number a whole number and in doing so I need to keep track of how many decimal places there were. ie change 28.9876 to 289874 and store there were 4 decimal places. I am currently doing this by converting to a string and checking how many are after the '.' This seems a poor way to solve this problem, but I am drawing a blank on other approaches. Can anyone offer other more efficient/elegant solutions?
this thing looks like it was written by an epileptic ferret Dave Kreskowiak
I'm sure there is an even better way, but...
decimal d = 10.1234;
int dp = (d - decimal.Truncate(d)).ToString().Length - 2;
int val = (int)(d * (decimal)Math.Pow(10, dp));
This method, although still converting it to a string, doesn't 'search' the string for a particular character... just gets its length... which is relatively efficient. As I said... there will be a better way... what it is I cannot immediately say. Hope this helps.Matthew Butler
-
I have a decimal number with a variable amount of digits following the decimal. I need to make the number a whole number and in doing so I need to keep track of how many decimal places there were. ie change 28.9876 to 289874 and store there were 4 decimal places. I am currently doing this by converting to a string and checking how many are after the '.' This seems a poor way to solve this problem, but I am drawing a blank on other approaches. Can anyone offer other more efficient/elegant solutions?
this thing looks like it was written by an epileptic ferret Dave Kreskowiak
For instance
decimal d = 28.9876M;
int[] bits = Decimal.GetBits(d);
int pow = (bits[3] >> 16) & 0xFF;At the end
pow
contains4
, i.e. the number of digits after the decimal point.If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
I have a decimal number with a variable amount of digits following the decimal. I need to make the number a whole number and in doing so I need to keep track of how many decimal places there were. ie change 28.9876 to 289874 and store there were 4 decimal places. I am currently doing this by converting to a string and checking how many are after the '.' This seems a poor way to solve this problem, but I am drawing a blank on other approaches. Can anyone offer other more efficient/elegant solutions?
this thing looks like it was written by an epileptic ferret Dave Kreskowiak
you can use
Decimal.GetBits(decimal)
as described here: http://msdn.microsoft.com/en-us/library/system.decimal.getbits(VS.71).aspx[^] edit: okay, too late again -
you can use
Decimal.GetBits(decimal)
as described here: http://msdn.microsoft.com/en-us/library/system.decimal.getbits(VS.71).aspx[^] edit: okay, too late againThanks for the help.
this thing looks like it was written by an epileptic ferret Dave Kreskowiak
-
For instance
decimal d = 28.9876M;
int[] bits = Decimal.GetBits(d);
int pow = (bits[3] >> 16) & 0xFF;At the end
pow
contains4
, i.e. the number of digits after the decimal point.If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeAppreciate the help
this thing looks like it was written by an epileptic ferret Dave Kreskowiak