BinaryFloatingPoint

public extension BinaryFloatingPoint
  • SwifterSwift: Returns a rounded value with the specified number of decimal places and rounding rule. If numberOfDecimalPlaces is negative, 0 will be used.

    let num = 3.1415927
    num.rounded(numberOfDecimalPlaces: 3, rule: .up) -> 3.142
    num.rounded(numberOfDecimalPlaces: 3, rule: .down) -> 3.141
    num.rounded(numberOfDecimalPlaces: 2, rule: .awayFromZero) -> 3.15
    num.rounded(numberOfDecimalPlaces: 4, rule: .towardZero) -> 3.1415
    num.rounded(numberOfDecimalPlaces: -1, rule: .toNearestOrEven) -> 3
    

    Declaration

    Swift

    func rounded(numberOfDecimalPlaces: Int, rule: FloatingPointRoundingRule) -> Self

    Parameters

    numberOfDecimalPlaces

    The expected number of decimal places.

    rule

    The rounding rule to use.

    Return Value

    The rounded value.