count decimal point
-
Very tricky. The problem is in the datatype you use to represent the number. Floating points don't accuratly represent the number they are supposed to. It could be off by a very small amount. Thus, if you have a number like 17.2, the internal representation may be 17.1999999. Your may want 1, because 17.2 is the real value, or you may want infinity, because the real value repeats for ever. How important is it that you can do this? Roy.
-
Can you provide more information? I did not understand what you meant by count the number!!
Thanks for your reply I need to count how many numbers after the decimal point for instance if 3.44 or 3.4 i need to count how many number after the decimal point it is now two number in the frist number and one number in the frist number. Thanks for your time Best regards, Hoho
-
Hello! One thing that comes to mind is to convert the number to a string and then search for the decimal separator:
int FractionDigits(double d)
{
string s = d.ToString();
int i = s.IndexOf(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
if (i<0)
return 0;
else
return s.Length-i-1;
}The maximum number you'll get depends on how
double.ToString()
formats your number. Usually you'll get up to 15 digits, but that's enough for most practical cases. Regards, mav