Number formatting in Scala?

前端 未结 7 2103
夕颜
夕颜 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:19

    var x:Double = 1
    var y:Double = 1.0
    
    print(x) // => 1.0
    print(y) // => 1.0
    

    If i understand you question you want scala to print x and y differently? The problem is that x and y are both a variable of the type Double and look the same.

    Why do you explicitly define the type of the vars?

    var x = 1 
    var y= 1.0
    
    print(x) // => 1
    print(y) // => 1.0
    

提交回复
热议问题