How to round a floating point number and remove digits after decimal point in Scala

1 Answer

0 votes
object Main {
  def main(args: Array[String]): Unit = {
    val x1 = 4.311
    val y1 = 6.5
    val z1 = 9.811

    println(f"${math.floor(x1)}%.3f")
    println(f"${math.floor(y1)}%.3f")
    println(f"${math.floor(z1)}%.3f")

    val x2 = -4.311
    val y2 = -6.5
    val z2 = -9.811

    println(f"${math.floor(x2)}%.3f")
    println(f"${math.floor(y2)}%.3f")
    println(f"${math.floor(z2)}%.3f")
  }
}



   
/*
           
run:
     
4.000
6.000
9.000
-5.000
-7.000
-10.000
       
*/

 



answered Sep 18, 2024 by avibootz
...