出于好奇,下面会有一个等效的Lambda表达式吗?
...刚开始使用lambda,对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++;
}
答案 0 :(得分:7)
double1.ToString().SkipWhile(c => c!='.').Skip(1).Count()
例如:
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;
答案 1 :(得分:1)
Math.Ceiling(-Math.Log(double_in, 10))
答案 2 :(得分:1)
我会写一个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的权力的方式除外)。
答案 3 :(得分:0)
如果认为这更有可能回答你的问题,并且文化不变。
Math.Max(0, num.ToString().Length - Math.Truncate(num).ToString().Length - 1)