Contents

precision(_:)

Modifies the format style to use the specified precision.

Declaration

func precision(_ p: IntegerFormatStyle<Value>.Configuration.Precision) -> IntegerFormatStyle<Value>

Parameters

  • p:

    The precision to apply to the format style.

Return Value

An integer format style modified to use the specified precision.

Discussion

The NumberFormatStyleConfiguration.Precision type lets you specify a fixed number of digits to show for a number’s integer and fractional parts, although IntegerFormatStyle only uses the former. You can also set a fixed number of significant digits.

The following example creates a default IntegerFormatStyle for the en_US locale, and a second style that uses a maximum of four significant digits. It then applies each style to an array of integers. The formatting that the modified style applies truncates precision to 0 after the fourth most significant digit.

let defaultStyle = IntegerFormatStyle<Int>(locale: Locale(identifier: "en_US"))
let precisionStyle = defaultStyle.precision(.significantDigits(1...4))
let nums = [123, 1234, 12345, 123456, 1234567]
let defaultNums = nums.map { defaultStyle.format($0) } // ["123", "1,234", "12,345", "123,456", "1,234,567"]
let precisionNums = nums.map { precisionStyle.format($0) } // ["123", "1,234", "12,340", "123,500", "1,235,000"]

See Also

Customizing style behavior