I have a variable of decimal
type and I want to check the number of digits before decimal point in it.
What should I do? For example, 467.45
should
Algorithm:
|decimal|
to String."."
exist in the decimal, cut before it, otherwise consider the whole number.Example:
3.14 --> 3.14 --> "3.14" --> "3.14".Substring(0,1) --> "3".Length --> 1
-1024 --> 1024 --> "1024" --> IndexOf(".") = -1 --> "1024" --> 4
Code:
static int getNumOfDigits (decimal num)
{
string d = Math.Abs(num).ToString();
if (d.IndexOf(".") > -1)
{
d = d.Substring(0, d.IndexOf("."));
}
return d.Length;
}