Getting the number of decimal places

Revision as of 14:45, 12 August 2020 by CalmoSoft (talk | contribs) (Created page with "{{task}} Write a program (function) to get the number of decimals in a given number. Examples: for num = 12.345 decimals = 3 and for num = 12.3450 decimals = 4 =={{header|...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Write a program (function) to get the number of decimals in a given number.

Task
Getting the number of decimal places
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>

  1. 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