C # in lambda - the number of decimal places / first significant decimal

Out of curiosity, would there be an equivalent Lambda expression for the following?

... just started using lambda, so still not familiar with methods like zip ...

//Pass in a double and return the number of decimal places
//ie. 0.00009 should result in 5

//EDIT: Number of decimal places is good.
//However, what I really want is the position of the first non-zero digit 
//after the decimal place.

int count=0;
while ((int)double_in % 10 ==0)
{
double_in*=10;
count++;
}
+5
source share
4 answers
 double1.ToString().SkipWhile(c => c!='.').Skip(1).Count()

For instance:

double double1 = 1.06696;
int count = double1.ToString().SkipWhile(c => c!='.').Skip(1).Count(); // count = 5;

double double2 = 16696;
int count2 = double2.ToString().SkipWhile(c => c!='.').Skip(1).Count(); // count = 0;
+7
source
Math.Ceiling(-Math.Log(double_in, 10))
+1
source

InfiniteSequence,

/// <summary>
/// Returns an inifinte sequence of integers starting with 1
/// </summary>
public static IEnumerable<int> InfiniteSequence() {
  int value = 0;
  while (true) {
    yield return ++value;
  }
}

( .NET:)...)

var count = InfiniteSequence().Select(i => (int)(double_in * Math.Power(10,i))).TakeWhile(v=>v%10==0).Count();

( , 10) .

+1

, .

Math.Max(0, num.ToString().Length - Math.Truncate(num).ToString().Length - 1)
0

All Articles