I want to calculate the number of digits after the decimal point of a decimal number. My program works, but it returns the wrong number when I add more digits, for example, 5.55266666666555555
has 17 digits after the decimal point, but my program returns only 15.
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Result is: {0}", howMuchDots(5.55266666666555555));
}
static double howMuchDots(double num)
{
/* Convert num to string, split it with dot into an array, and take the second cell. Then get the length of the string */
return num.ToString().Split(".")[1].Length;
}
}
I tried to use float
instead, but it doesn't work either.