Fixing Scala error on String.format: overloaded method value format with alternatives:

怼烎@ 2021-12-11 16:19 304阅读 0赞

类型上需要一致, 统一成java 的。

  1. If you try to use a Double in scala, you may get errors passing it to Java:
  2. String.format("%-30s: %.2f ± %0.2f", kv._1, kv._2, kv._3)
  3. This is the error I got:
  4. [error] D:\projects\scala-indexer\src\main\scala\indexer\GpuConcepts.scala:114: overloaded method value format with alternatives:
  5. [error] (x$1: java.util.Locale,x$2: String,x$3: Object*)String <and>
  6. [error] (x$1: String,x$2: Object*)String
  7. [error] cannot be applied to (String, String, Double, Double)
  8. [error] String.format("%-30s: %.2f ▒ %0.2f", kv._1, kv._2, kv._3)
  9. The issue here is that Double”, as in scala.Double is the equivalent of the primitive double in Java. You can fix it by boxing the value:
  10. String.format("%-30s: %.2f ± %0.2f", kv._1, Double.box(kv._2), Double.box(kv._3))

发表评论

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

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

相关阅读