Getting the number of decimal places: Difference between revisions

Content added Content deleted
Line 165: Line 165:
π has 15 decimals.
π has 15 decimals.
</pre>
</pre>

=={{header|Kotlin}}==
{{trans|Java}}
<lang scala>fun findNumOfDec(x: Double): Int {
val str = x.toString()
if (str.endsWith(".0")) {
return 0
}
return str.substring(str.indexOf('.')).length - 1
}

fun main() {
for (n in listOf(12.0, 12.345, 12.345555555555, 12.3450, 12.34555555555555555555, 1.2345e+54)) {
println("%f has %d decimals".format(n, findNumOfDec(n)))
}
}</lang>
{{out}}
<pre>12.000000 has 0 decimals
12.345000 has 3 decimals
12.345556 has 12 decimals
12.345000 has 3 decimals
12.345556 has 15 decimals
1234500000000000000000000000000000000000000000000000000.000000 has 7 decimals</pre>


=={{header|Perl}}==
=={{header|Perl}}==