File: 0008-notation-formatting-for-currencies.md

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (82 lines) | stat: -rw-r--r-- 2,978 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Support notation when formatting currencies

* Proposal: [SF-0008](0008-notation-formatting-for-currencies.md)
* Authors: [Jacob Lukas](https://github.com/jacoblukas)
* Review Manager: [Tina Liu](https://github.com/itingliu)
* Status: **Accepted**
* Bug: [apple/swift-foundation#379](https://github.com/apple/swift-foundation/issues/379)
* Implementation: [apple/swift-foundation#449](https://github.com/apple/swift-foundation/pull/449)
* Review: ([pitch](https://forums.swift.org/t/pitch-support-notation-when-formatting-currencies/70223))

## Introduction/Motivation

When formatting numbers or percents, we can specify a notation, for example:

```swift
let count = 1_500_000
let result = count.formatted(.number.notation(.compactName))
assert(result == "1.5M")
```

We will enable this for currency formatters as well.

## Proposed solution

Add the `notation` modifier to the various currency formatters. All of the notations supported by the number and percent formatters will be supported by the currency formatters.

```swift
let price = Decimal(1_500_000.59)
let result = price.formatted(.currency(code: "USD").notation(.compactName))
assert(result == "$1.5M")
```

The underlying ICU formatters already support this, so we just have to forward the appropriate options.

## Detailed design

```swift
extension IntegerFormatStyle.Currency {
    /// Modifies the format style to use the specified notation.
    ///
    /// - Parameter notation: The notation to apply to the format style.
    /// - Returns: An integer currency format style modified to use the specified notation.
    @available(FoundationPreview 0.4, *)
    public func notation(_ notation: Configuration.Notation) -> Self
}

extension FloatingPointFormatStyle.Currency {
    /// Modifies the format style to use the specified notation.
    ///
    /// - Parameter notation: The notation to apply to the format style.
    /// - Returns: A floating-point currency format style modified to use the specified notation.
    @available(FoundationPreview 0.4, *)
    public func notation(_ notation: Configuration.Notation) -> Self
}

extension Decimal.FormatStyle.Currency {
    /// Modifies the format style to use the specified notation.
    ///
    /// - Parameter notation: The notation to apply to the format style.
    /// - Returns: A decimal currency format style modified to use the specified notation.
    @available(FoundationPreview 0.4, *)
    public func notation(_ notation: Configuration.Notation) -> Self
}

extension CurrencyFormatStyleConfiguration {
    /// The type used to configure notation for currency format styles.
    @available(FoundationPreview 0.4, *)
    public typealias Notation = NumberFormatStyleConfiguration.Notation
}
```

## Source compatibility

These changes are additive only and are not expected to have an impact on source compatibility.

## Implications on adoption

This new API will have FoundationPreview 0.4 availability.

## Alternatives considered

None.