Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# to Lambda - count decimal places / first significant decimal

Tags:

c#

lambda

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

... just started using lambda so not familiar yet 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++;
}
like image 402
CMH Avatar asked Mar 07 '11 13:03

CMH


1 Answers

 double1.ToString().SkipWhile(c => c!='.').Skip(1).Count()

For example:

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;
like image 111
Aliostad Avatar answered Sep 20 '22 02:09

Aliostad