【问题标题】:Rounding in Swift with round()在 Swift 中使用 round() 进行舍入
【发布时间】:2014-10-20 05:22:09
【问题描述】:

在玩的过程中,我发现了 swift 中的 round() 函数。可以如下使用:

round(0.8)

这将返回 1,正如预期的那样。这是我的问题:

如何快速四舍五入?

我希望能够插入一个数字,比如 0.6849,然后得到 0.685。 round() 如何做到这一点?或者,不是吗,在这种情况下,有什么功能?

【问题讨论】:

  • round(1000 * x) / 1000
  • 在最简单的情况下,使用round(x * 1000) / 1000。另请参阅 this answer to a similar question 以找到使用 NSDecimalNumberNSNumberFormatterString initializer 等对双精度进行舍入的其他方法。

标签: swift xcode6 rounding


【解决方案1】:

你可以这样做:

round(1000 * x) / 1000

【讨论】:

  • 为 SWIFT 3 更新:someval.rounded()
【解决方案2】:

更新答案

round(someDecimal) 是旧的 C 风格。从 Swift 3 开始,双精度和浮点数具有内置的 Swift 函数。

var x = 0.8
x.round() // x is 1.0 (rounds x in place)

var x = 0.8
var y = x.rounded() // y is 1.0, x is 0.8

请参阅我的回答 fuller answer here(或 here),了解更多关于如何使用不同的 rounding rules 的详细信息。

正如其他答案所指出的,如果你想四舍五入到千分之一,那么在四舍五入之前暂时乘以1000

【讨论】:

    【解决方案3】:
    func round(value: Float, decimalPlaces: UInt) {
      decimalValue = pow(10, decimalPlaces)
      round(value * decimalValue) / decimalValue
    }
    …
    func round(value: CGFloat, decimalPlaces: UInt)
    func round(value: Double, decimalPlaces: UInt)
    func roundf(value: Float, decimalPlaces: UInt)
    

    【讨论】:

    • ` static func round_pow_2( value: CGFloat, decimalPlaces: CGFloat ) -> CGFloat { let decimalValue = pow( 2.0, decimalPlaces ) return round( value * decimalValue ) / decimalValue }`
    • 可能只有我,但那个功能没有多大意义?
    【解决方案4】:

    这是一种方法。您可以轻松地为 Float 执行此操作,或者可能将其设为通用,以便适用于其中任何一个。

    public extension CGFloat {
        func roundToDecimals(decimals: Int = 2) -> CGFloat {
            let multiplier = CGFloat(10^decimals)
            return round(multiplier * self) / multiplier
        }
    }
    

    【讨论】:

    • 10^decimal 进行按位异或。应将其替换为 pow 或应使用 powf。
    【解决方案5】:

    斯威夫特 4:

    (x/1000).rounded()*1000
    

    【讨论】:

    • 如果x 是原始问题中给出的 0.6849,这将给出 0。应该是(x*1000).rounded()/1000
    【解决方案6】:

    这将四舍五入为不受 10 次方限制的任何值。

    extension Double {
        func roundToNearestValue(value: Double) -> Double {
            let remainder = self % value
            let shouldRoundUp = remainder >= value/2 ? true : false
            let multiple = floor(self / value)
            let returnValue = !shouldRoundUp ? value * multiple : value * multiple + value
            return returnValue
        }
    }
    

    【讨论】:

      【解决方案7】:
      var a=1.2344 
      var b:String = String(format: "%0.2f",a) 
      print(b)
      

      输出:

      1.23
      

      你得到的输出是字符串类型的。因此,如果需要,您需要转换为其他类型。下面的链接告诉你如何转换:

      https://supereasyapps.com/blog/2015/9/28/how-to-convert-strings-into-double-and-float-values-using-swift-2#:~:text=Convert%20Swift%20String%20to%20Double,(Double(%22200.0%22)!)

      【讨论】:

      【解决方案8】:

      查看 Apple 的 round()rounded(_:) 文档。

      【讨论】:

        猜你喜欢
        • 2012-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-07
        • 2010-09-08
        • 2015-01-23
        • 2014-09-15
        • 1970-01-01
        相关资源
        最近更新 更多