如何将序列传递给在 Scala 中接受多个参数的函数

How to pass a sequence to a function accepting multiple arguments in Scala

下面是我的 2 个函数

def getProductAsDouble(nums: Double*):Double={
    var n = new BigDecimal("1.0")
    nums.foreach(num => n = n.multiply(new BigDecimal(num.toString)))
    n.setScale(2,java.math.BigDecimal.ROUND_HALF_UP).doubleValue()
  }

  def getProductAsString(nf:NumberFormat,nums: Double*): String ={
    nf.format(getProductAsDouble(nums)) // shows error here
  }

我无法将 numsgetProductAsString 传递到 getProductAsDouble。有什么办法可以解决这个问题吗?

您可以使用: _*

例如

def getProductAsString(nf:NumberFormat,nums: Double*): String ={
  nf.format(getProductAsDouble(nums : _*))
}

这里对_*类型注解有很好的解释:What does `:_*` (colon underscore star) do in Scala?