Getting the number of decimal places
Write a program (function) to get the number of decimals in a given number.
Getting the number of decimal places
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
Examples:
for num = 12.345 decimals = 3 and for num = 12.3450 decimals = 4
Ring
<lang ring>
- Testing the function
decimals(2) # Unsensitive to the default setting of decimals n = 5.1945 ? NbrOfDecimals(n) # Gives 4
func NbrOfDecimals(n) nTemp = 1 nNbrOfDecimals = 0 while True if nNbrOfDecimals < 9 nNbrOfDecimals++ nTemp *= 10 nTemp1 = n * nTemp - ceil( n * nTemp ) if nTemp1 = 0 return nNbrOfDecimals ok else raise("Acceeding the maximum number of 9 decimals!") ok end </lang>
- Output:
4