overloaded method value format with alternatives: (x$1: java.util.Locale,x$2: String,x$3: Object*

矫情吗;* 2021-11-17 11:48 439阅读 0赞
  1. test("String.formatted in scala Double") {
  2. val a:Double = 1.200001
  3. val b:Double = 1.3000001
  4. println(String.format("%.2f , %.2f", a, b)) // 这里报错。。。。。。。。。
  5. println(String.format("%.2f , %.2f", Double.box(a), Double.box(b)))
  6. println(Double.box(a).formatted("%.2f") + " , " + Double.box(b).formatted("%.2f"))
  7. }

报错如下:

  1. Error:(29, 24) overloaded method value format with alternatives:
  2. (x$1: java.util.Locale,x$2: String,x$3: Object*)String <and>
  3. (x$1: String,x$2: Object*)String
  4. cannot be applied to (String, Double, Double)
  5. println(String.format("%.2f , %.2f", a, b))

解决办法:
println(String.format(“%.2f , %.2f”, Double.box(a), Double.box(b)))
println(Double.box(a).formatted(“%.2f”) + “ , “ + Double.box(b).formatted(“%.2f”))

发表评论

表情:
评论列表 (有 0 条评论,439人围观)

还没有评论,来说两句吧...

相关阅读