Getting the number of decimal places: Difference between revisions
Content added Content deleted
(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|...") |
No edit summary |
||
Line 1: | Line 1: | ||
{{task}} |
{{draft task}} |
||
Write a program (function) to get the number of decimals in a given number. |
Write a program (function) to get the number of decimals in a given number. |
Revision as of 14:47, 12 August 2020
Getting the number of decimal places is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
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
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