繁体   English   中英

C# 后数小数位数。 在第一个数字之前

[英]C# Count decimal digits after . and before the first number

我是 c# 的新手,希望有人能帮助我。
我只是想知道,是否有更简单快捷的方法来解决我的计算。

示例:我有 0.001,我想计算点之后和 1 之前有多少 0...
由于 2x 00,结果也是 2。

这是 atm 我的计算器;

decimal test = 0.001m;
var test1 = Convert.ToString(test).Split('1');
var test2 = test1[0].Split('.');
int test3 = test2[1].Count(Char.IsDigit);
test3 = 2

谢谢:)

另一种可能的解决方案:

decimal test = 0.00007m;
Int32 c = -1;
while (test < 1) { test *= 10; c++; }
Console.WriteLine($"Zero number: {c}");

Output:4

您可以使用以下方法计算点后数:

test.SubString(num.IndexOf(".") + 1).Length;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM