Contents

rounded(rule:increment:)

Modifies the format style to use the specified rounding rule and increment.

Declaration

func rounded(rule: IntegerFormatStyle<Value>.Configuration.RoundingRule = .toNearestOrEven, increment: Int? = nil) -> IntegerFormatStyle<Value>

Parameters

  • rule:

    The rounding rule to apply to the format style.

  • increment:

    A multiple by which the formatter rounds the fractional part. The formatter produces a value that is an even multiple of this increment. If this parameter is nil (the default), the formatter doesn’t apply an increment.

Return Value

An integer format style modified to use the specified rounding rule and increment.

Discussion

The following example creates a default IntegerFormatStyle for the en_US locale, and a modified style that rounds integers to the nearest multiple of 100. It then formats the value 1999 using these format styles.

let defaultStyle = IntegerFormatStyle<Int>(locale: Locale(identifier: "en_US"))
let roundedStyle = defaultStyle.rounded(rule: .toNearestOrEven,
                                        increment: 100)
let num = 1999
let defaultNum = num.formatted(defaultStyle) // "1,999"
let roundedNum = num.formatted(roundedStyle) // "2,000"

See Also

Customizing style behavior