Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find number of decimal places in decimal value regardless of culture

I'm wondering if there is a concise and accurate way to pull out the number of decimal places in a decimal value (as an int) that will be safe to use across different culture info?

For example:
19.0 should return 1,
27.5999 should return 4,
19.12 should return 2,
etc.

I wrote a query that did a string split on a period to find decimal places:

int priceDecimalPlaces = price.ToString().Split('.').Count() > 1                    ? price.ToString().Split('.').ToList().ElementAt(1).Length                    : 0; 

But it occurs to me that this will only work in regions that use the '.' as a decimal separator and is therefore very brittle across different systems.

like image 953
Jesse Carter Avatar asked Nov 20 '12 16:11

Jesse Carter


People also ask

How do you find the number of decimal places?

The first digit after the decimal represents the tenths place. The next digit after the decimal represents the hundredths place. The remaining digits continue to fill in the place values until there are no digits left. The number.

How many decimal places should be used in a research?

A suitable rule specifies up to one decimal place and up to two significant digits. When comparing group means or percentages in tables, rounding should not blur the differences between them.

How many decimal places are used in descriptive statistics?

Abstract. Reporting descriptive statistics requires rounding the results. Experienced researchers typically round the numbers to one or two decimals, following the APA manual. However, this general recommendation ignores the sample size and the instrument's precision.


2 Answers

I used Joe's way to solve this issue :)

decimal argument = 123.456m; int count = BitConverter.GetBytes(decimal.GetBits(argument)[3])[2]; 
like image 98
burning_LEGION Avatar answered Oct 05 '22 23:10

burning_LEGION


Since none of the answers supplied were good enough for the magic number "-0.01f" converted to decimal.. i.e: GetDecimal((decimal)-0.01f);
I can only assume a colossal mind-fart virus attacked everyone 3 years ago :)
Here is what seems to be a working implementation to this evil and monstrous problem, the very complicated problem of counting the decimal places after the point - no strings, no cultures, no need to count the bits and no need to read math forums.. just simple 3rd grade math.

public static class MathDecimals {     public static int GetDecimalPlaces(decimal n)     {         n = Math.Abs(n); //make sure it is positive.         n -= (int)n;     //remove the integer part of the number.         var decimalPlaces = 0;         while (n > 0)         {             decimalPlaces++;             n *= 10;             n -= (int)n;         }         return decimalPlaces;     } } 

private static void Main(string[] args) {     Console.WriteLine(1/3m); //this is 0.3333333333333333333333333333     Console.WriteLine(1/3f); //this is 0.3333333      Console.WriteLine(MathDecimals.GetDecimalPlaces(0.0m));                  //0     Console.WriteLine(MathDecimals.GetDecimalPlaces(1/3m));                  //28     Console.WriteLine(MathDecimals.GetDecimalPlaces((decimal)(1 / 3f)));     //7     Console.WriteLine(MathDecimals.GetDecimalPlaces(-1.123m));               //3     Console.WriteLine(MathDecimals.GetDecimalPlaces(43.12345m));             //5     Console.WriteLine(MathDecimals.GetDecimalPlaces(0));                     //0     Console.WriteLine(MathDecimals.GetDecimalPlaces(0.01m));                 //2     Console.WriteLine(MathDecimals.GetDecimalPlaces(-0.001m));               //3     Console.WriteLine(MathDecimals.GetDecimalPlaces((decimal)-0.00000001f)); //8     Console.WriteLine(MathDecimals.GetDecimalPlaces((decimal)0.0001234f));   //7     Console.WriteLine(MathDecimals.GetDecimalPlaces((decimal)0.01f));        //2     Console.WriteLine(MathDecimals.GetDecimalPlaces((decimal)-0.01f));       //2 } 
like image 43
G.Y Avatar answered Oct 06 '22 01:10

G.Y