Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 🙂

>Solution :

Another possible solution:

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

Output: 4

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading