【问题标题】:Round Half Down in Swift斯威夫特的四舍五入
【发布时间】:2019-08-08 13:28:39
【问题描述】:

Swift 中是否存在与 Java 中的 ROUND_HALF_DOWN 行为相同的舍入模式?

舍入模式向“最近的邻居”舍入,除非两个邻居等距,在这种情况下向下舍入。如果丢弃的分数 > 0.5,则行为与 RoundingMode.UP 相同;否则,行为与 RoundingMode.DOWN 相同。

例子:

  • 2.5 向下舍入为 2.0
  • 2.6 向上取整到 3.0
  • 2.4 向下舍入为 2.0

对于负数:

  • -2.5 向上取整为 -2.0
  • -2.6 向下舍入为 -3.0
  • -2.4 向上取整为 -2.0

【问题讨论】:

    标签: swift rounding


    【解决方案1】:

    是的,你可以使用 NSNumberFormatter 和 RoundingMode 做类似的事情

    在这里阅读

    NSNumberFormatter

    RoundingMode

    【讨论】:

      【解决方案2】:

      Swift 用规则实现.round() 函数,根据Apple

      FloatingPointRoundingRule

      case awayFromZero
      

      四舍五入到最接近的允许值,其幅度大于或等于源的幅度。

      case down
      

      四舍五入到小于或等于源的最接近的允许值。

      case toNearestOrAwayFromZero
      

      四舍五入到最接近的允许值;如果两个值同样接近,则选择幅度较大的值。

      case toNearestOrEven
      

      四舍五入到最接近的允许值;如果两个值同样接近,则选择偶数。

      case towardZero
      

      四舍五入到最接近的允许值,其幅度小于或等于源的幅度。

      case up
      

      四舍五入到大于或等于源的最接近的允许值。

      【讨论】:

        【解决方案3】:

        正如@MohmmadS 所说,这些是内置的舍入方法。

        您可以像这样实现自定义舍入:

        func round(_ value: Double, toNearest: Double) -> Double {
            return round(value / toNearest) * toNearest
        }
        
        func roundDown(_ value: Double, toNearest: Double) -> Double {
            return floor(value / toNearest) * toNearest
        }
        
        func roundUp(_ value: Double, toNearest: Double) -> Double {
            return ceil(value / toNearest) * toNearest
        }
        

        例子:

        round(52.376, toNearest: 0.01) // 52.38
        round(52.376, toNearest: 0.1)  // 52.4
        round(52.376, toNearest: 0.25) // 52.5
        round(52.376, toNearest: 0.5)  // 52.5
        round(52.376, toNearest: 1)    // 52
        

        【讨论】:

        • 我认为问题是“Swift 中是否有舍入模式”,而不是如何实现
        • @MohmmadS 你是对的,但也许他正在寻找解决方案!
        【解决方案4】:
         var a = 6.54
                a.round(.toNearestOrAwayFromZero)
                // a == 7.0
        
                var b = 6.54
                b.round(.towardZero)
                // b == 6.0
        
        
                var c = 6.54
                c.round(.up)
                // c == 7.0
        
        
                var d = 6.54
                d.round(.down)
                // d == 6.0
        

        您也可以这样做,但也需要取小数点后的值。

        【讨论】:

          【解决方案5】:

          据我所知,没有 FloatingPointRoundingRule 与 Java 的 ROUND_HALF_DOWN 具有相同的行为,但您可以通过 rounded()nextDownnextUp 的组合获得结果:

          func roundHalfDown(_ x: Double) -> Double {
              if x >= 0 {
                  return x.nextDown.rounded()
              } else {
                  return x.nextUp.rounded()
              }
          }
          

          例子:

          print(roundHalfDown(2.4)) // 2.0
          print(roundHalfDown(2.5)) // 2.0
          print(roundHalfDown(2.6)) // 3.0
          
          print(roundHalfDown(-2.4)) // -2.0
          print(roundHalfDown(-2.5)) // -2.0
          print(roundHalfDown(-2.6)) // -3.0
          

          或者作为一个通用的扩展方法,这样就可以和所有的浮点类型一起使用了(FloatDoubleCGFloat):

          extension FloatingPoint {
              func roundedHalfDown() -> Self {
                  return self >= 0 ? nextDown.rounded() : nextUp.rounded()
              }
          }
          

          例子:

          print((2.4).roundedHalfDown()) // 2.0
          print((2.5).roundedHalfDown()) // 2.0
          print((2.6).roundedHalfDown()) // 3.0
          
          print((-2.4).roundedHalfDown()) // -2.0
          print((-2.5).roundedHalfDown()) // -2.0
          print((-2.6).roundedHalfDown()) // -3.0
          

          【讨论】:

            猜你喜欢
            • 2017-07-22
            • 2017-03-24
            • 1970-01-01
            • 2016-02-08
            • 2017-11-13
            • 2015-08-19
            • 2014-09-11
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多