FloatingPointParseStrategy
A parse strategy for creating floating-point values from formatted strings.
Declaration
struct FloatingPointParseStrategy<Format> where Format : FormatStyle, Format.FormatInput : BinaryFloatingPointOverview
Create an explicit FloatingPointParseStrategy to parse multiple strings according to the same parse strategy. In the following example, usCurrencyStrategy is a FloatingPointParseStrategy that uses US dollars and the en_US locale’s conventions for number formatting. The example then uses this strategy to parse an array of strings, some of which represent valid US currency values.
let usCurrencyStrategy: FloatingPointParseStrategy =
FloatingPointFormatStyle<Double>.Currency(code: "USD",
locale: Locale(identifier: "en_US"))
.parseStrategy
let currencyValues = ["$100.11", "$1,000.22", "$10,000.33", "€100.44"]
let parsedValues = currencyValues.map { try? usCurrencyStrategy.parse($0) } // [Optional(100.11), Optional(1000.22), Optional(10000.33), nil]You don’t need to instantiate a parse strategy variable to parse a single string. Instead, use the BinaryFloatingPoint initializers that take a source String and a format parameter to parse the string according to the provided FormatStyle. The following example parses a string that represents a currency value in US dollars.
let formattedUSDollars = "$1,234.56"
let parsedUSDollars = try? Double(formattedUSDollars, format: .currency(code: "USD")
.locale(Locale(identifier: "en_US"))) // 1234.56