【发布时间】:2017-01-17 22:07:38
【问题描述】:
在 swift 中使用双精度时需要消除额外的零,例如(3.0 应该像 3 一样输出,3.2 应该是 3.2)
//description is String; operand is Double
// works
(operand - floor(operand) != 0) ? (description += String(operand)) : (description += String(Int(operand)))
// not works
description += String( (operand - floor(operand) != 0) ? operand : Int(operand) )
为什么三元运算符在第二个版本中会出错?有没有其他方法可以避免重复代码?
【问题讨论】:
-
当您发布有关错误的问题时,您需要在问题中包含完整且准确的错误。