C#查找数字的位数
我想数一数小数点后的数字。 我需要的结果是,例如:C#查找数字的位数,c#,math,numbers,C#,Math,Numbers,我想数一数小数点后的数字。 我需要的结果是,例如: float n = 1,5674; // print There are 4 digits behind the decimal point. 我已经知道了。但我不知道如何让它和小数一起工作 toTest = 1,5674 int i = 0; do { toTest = toTest / 10; i++; } while(Math.Abs(toTest) >= 1); 可以很好地找到大于1的数字的长度。 但我不知
float n = 1,5674; // print There are 4 digits behind the decimal point.
我已经知道了。但我不知道如何让它和小数一起工作
toTest = 1,5674
int i = 0;
do
{
toTest = toTest / 10;
i++;
}
while(Math.Abs(toTest) >= 1);
可以很好地找到大于1的数字的长度。
但我不知道如何计算小数位
我是一名实习生,所以如果我监督正确的解决方案,请不要杀了我
致以最诚挚的问候不要除以,而是乘以10
:
double toTest = 1.5674;
int i = 0;
// note "toTest *= 10" instead of original "toTest = toTest / 10"
for (; toTest - Math.Floor(toTest) != 0; toTest *= 10) 10;
i += 1;
Console.Write(i); // 4
你是个实习生没问题。这是一个你似乎没有找到解决办法的问题。我在一次网络搜索中发现了三个副本;如果这些对你没有帮助,请用你的问题来说明你已经尝试了什么。@CodeCaster我明白了。下次我会更彻底。很抱歉