How to get the first digit after the decimal point of a float number in Scala

1 Answer

0 votes
object Main {

  def firstDecimalDigit(f: Float): Int =
    (math.floor(math.abs(f) * 10).toInt) % 10

  def main(args: Array[String]): Unit = {
    val f = 983.6571f
    println(firstDecimalDigit(f)) // 6
  }
}


/*
run:

6

*/

 



answered Oct 30, 2024 by avibootz
edited Sep 16 by avibootz

Related questions

...