Number formatting in Scala?

前端 未结 7 2397
夕颜
夕颜 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条回答
  •  被撕碎了的回忆
    2021-02-18 23:28

    If you are working with a Double and want to format it as a String without .0 when it's a whole number and with its decimals otherwise, then you could use String::stripSuffix:

    x.toString.stripSuffix(".0")
    // val x: Double = 1.34    =>   "1.34"
    // val x: Double = 1.0     =>   "1"
    

提交回复
热议问题