format(_:)
Formats an integer, using this style.
Declaration
func format(_ value: Value) -> StringParameters
- value:
The integer to format.
Return Value
A string representation of value, formatted according to the style’s configuration.
Discussion
Use this method when you want to create a single style instance and use it to format multiple integers. The following example creates a style that uses the en_US locale, and adds the compactName modifier. It applies this style to all the integers in an array.
let compactNameStyle = IntegerFormatStyle<Int>(
locale: Locale(identifier: "en_US"))
.notation(.compactName)
let nums = [100, 1000, 10000, 100000, 1000000]
let formattedNums = nums.map { compactNameStyle.format($0) } // ["100", "1K", "10K", "100K", "1M"]
To format a single integer, use the BinaryInteger instance method formatted(_:), passing in an instance of IntegerFormatStyle, or formatted() to use a default style.