Skip to content

Instance Method

sign(strategy:)

iOS 15.0+ iPadOS 15.0+ Mac Catalyst 15.0+ macOS 12.0+ tvOS 15.0+ watchOS 8.0+
Modifies the format style to use the specified sign display strategy for displaying or omitting sign symbols.

Parameters

strategy

The sign display strategy to apply to the format style.

Return Value

A decimal format style modified to use the specified sign display strategy.

Discussion

The following example creates a default Decimal.FormatStyle for the en_US locale, and a second style that displays a sign for all values except zero. It then applies each style to an array of decimal values. The formatting that the modified style applies adds the negative (-) or positive (+) sign to all the numbers.

let defaultStyle = Decimal.FormatStyle(locale: Locale(identifier: "en_US"))
let alwaysStyle = defaultStyle.sign(strategy: .always(includingZero: false))
let nums: [Decimal] = [-2.1, -1.2, 0, 1.4, 2.5]
let defaultNums = nums.map { defaultStyle.format($0) } // ["-2.1", "-1.2", "0", "1.4", "2.5"]
let alwaysNums = nums.map { alwaysStyle.format($0) } // ["-2.1", "-1.2", "0", "+1.4", "+2.5"]