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

Viewed 55

I'm really new in c#, and I hope anyone can help me.
I just want to to know, if there is a easier and faster way to solve my calculations.

Example: I have 0.001, I want to Count how much 0 after the dot and before the 1 number...
Also the result is 2, because of 2x 00.

This is atm my Calc;

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

Thanks :)

2 Answers

Another possible solution:

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

Output: 4

You can count after dot using this:

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

Related