Some other answers related to this question involve converting a number into a string, and then using the character position of a βdotβ as an indicator of the number of decimal places. But this is not a reliable way to do this and will lead to wildly inaccurate answers if the number was many decimal places, and its conversion to a string contained exponential notation.
For example, for equation 1/11111111111111111 (one divided by 17 units), the string conversion is "9E-17", which means that the resulting answer is 5 when it should be 17. Of course, you can extract the correct answer from the end of the line when present "E-", but why do all this when it could be done mathematically instead?
Here is the function I just prepared for this. This is not an ideal solution, and I have not tested it completely, but it seems to work.
Public Function CountOfDecimalPlaces(ByVal inputNumber As Variant) As Integer ' ' This function returns the count of deciml places in a number using simple math and a loop. The ' input variable is of the Variant data type, so this function is versatile enougfh to work with ' any type of input number. ' CountOfDecimalPlaces = 0 'assign a default value of zero inputNumber = VBA.CDec(inputNumber) 'convert to Decimal for more working space inputNumber = inputNumber - VBA.Fix(inputNumber) 'discard the digits left of the decimal Do While inputNumber <> VBA.Int(inputNumber) 'when input = Int(input), it done CountOfDecimalPlaces = CountOfDecimalPlaces + 1 'do the counting inputNumber = inputNumber * 10 'move the decimal one place to the right Loop 'repeat until no decimal places left End Function
source share