Number formatting in Scala?

前端 未结 7 2100
夕颜
夕颜 2021-02-18 23:09

I have a dynamically changing input reading from a file. The numbers are either Int or Double. Why does Scala print .0 after every D

7条回答
  •  -上瘾入骨i
    2021-02-18 23:31

    Use printf:

    printf("The value is %.0f", x)
    

    For a description of the format string, see this page from the Java SE 6 API documentation.

    Note that you can ofcourse also use the Java library from Scala, so other ways to format numbers from Java can also be used from Scala. You can for example use class java.text.DecimalFormat:

    val df = new java.text.DecimalFormat("#####")
    println(df.format(x))
    

提交回复
热议问题