0

i want to count the digits behind the decimal point. The result i need is eg:

float n = 1,5674; // print There are 4 digits behind the decimal point. 

I already got this. But I don't know how to make it work with decimals.

toTest = 1,5674
int i = 0;

do
{
    toTest = toTest / 10;
    i++;
}
while(Math.Abs(toTest) >= 1);

works perfectly fine, to find the lenght of a number >1. But i can't figure out how to count the decimal places.

I'm a trainee, so please don't butcher me if I overseen the right solution.

best regards

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
br41n4ch3
  • 7
  • 1
  • 2
    It's no problem that you're a trainee. It is a problem that you don't appear to have looked for a solution. I've found three duplicates with a single web search; please [edit] your question to show what you have tried if those don't help you. – CodeCaster Oct 01 '19 at 11:13
  • @CodeCaster I see. I'll be more thoroughly next time. Sorry – br41n4ch3 Oct 01 '19 at 11:42

1 Answers1

0

Instead of dividing, multiply by 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
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215