代码之家  ›  专栏  ›  技术社区  ›  Ozgur Vatansever

在斯威夫特里把一半的人围起来

  •  -1
  • Ozgur Vatansever  · 技术社区  · 6 年前

    Swift中是否有一种与 ROUND_HALF_DOWN 在爪哇?

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

    例子:

    • 2.5舍入到2.0

    对于负数:

    • -2.5舍入到-2.0
    • -2.4四舍五入到-2.0
    5 回复  |  直到 6 年前
        1
  •  5
  •   Martin R    6 年前

    我只能说没有 FloatingPointRoundingRule 与Java的行为相同 ROUND_HALF_DOWN ,但您可以使用 rounded() nextDown nextUp

    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
    

    或者作为泛型扩展方法,以便它可以用于所有浮点类型( Float , Double CGFloat ):

    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
    
        2
  •  1
  •   Mohmmad S    6 年前

    快速工具 .round() Apple

    浮点取整规则

    case awayFromZero
    

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

    case down
    

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

    case toNearestOrAwayFromZero
    

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

    case toNearestOrEven
    

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

    case towardZero
    

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

    case up
    

        3
  •  0
  •   Bhavin Kansagara    6 年前

    是的,您可以使用NSNumberFormatter和RoundingMode执行类似的操作

    在这里读

    NSNumberFormatter

    RoundingMode

        4
  •  0
  •   Deviyani Swami    6 年前
     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
  •  0
  •   Arash Etemad    6 年前

    正如@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