@@ -122,8 +122,12 @@ open class NumberFormatter : Formatter {
122
122
123
123
private func _setFormatterAttributes( _ formatter: CFNumberFormatter ) {
124
124
if numberStyle == . currency {
125
- let symbol = _currencySymbol ?? _currencyCode ?? locale. currencySymbol ?? locale. currencyCode
126
- _setFormatterAttribute ( formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: symbol? . _cfObject)
125
+ let symbol = _currencySymbol ?? locale. currencySymbol
126
+ _setFormatterAttribute ( formatter, attributeName: kCFNumberFormatterCurrencySymbol, value: symbol? . _cfObject)
127
+
128
+ if let code = _currencyCode, code. count == 3 {
129
+ _setFormatterAttribute ( formatter, attributeName: kCFNumberFormatterCurrencyCode, value: code. _cfObject)
130
+ }
127
131
}
128
132
if numberStyle == . currencyISOCode {
129
133
let code = _currencyCode ?? _currencySymbol ?? locale. currencyCode ?? locale. currencySymbol
@@ -152,10 +156,8 @@ open class NumberFormatter : Formatter {
152
156
_setFormatterAttribute ( formatter, attributeName: kCFNumberFormatterRoundingMode, value: _roundingMode. rawValue. _bridgeToObjectiveC ( ) . _cfObject)
153
157
_setFormatterAttribute ( formatter, attributeName: kCFNumberFormatterRoundingIncrement, value: _roundingIncrement? . _cfObject)
154
158
155
- var width : Int = 0
156
- CFNumberGetValue ( _formatWidth. _bridgeToObjectiveC ( ) . _cfObject, kCFNumberLongType, & width)
157
- _setFormatterAttribute ( formatter, attributeName: kCFNumberFormatterFormatWidth, value: _formatWidth. _bridgeToObjectiveC ( ) . _cfObject)
158
- if width > 0 {
159
+ _setFormatterAttribute ( formatter, attributeName: kCFNumberFormatterFormatWidth, value: _formatWidth? . _bridgeToObjectiveC ( ) . _cfObject)
160
+ if self . formatWidth > 0 {
159
161
_setFormatterAttribute ( formatter, attributeName: kCFNumberFormatterPaddingCharacter, value: _paddingCharacter? . _cfObject)
160
162
_setFormatterAttribute ( formatter, attributeName: kCFNumberFormatterPaddingPosition, value: _paddingPosition. rawValue. _bridgeToObjectiveC ( ) . _cfObject)
161
163
} else {
@@ -192,10 +194,10 @@ open class NumberFormatter : Formatter {
192
194
// to indicate to use the default value (if nil) or the caller-supplied value (if not nil).
193
195
private func defaultMinimumIntegerDigits( ) -> Int {
194
196
switch numberStyle {
195
- case . none , . ordinal, . spellOut, . currencyPlural, . scientific :
197
+ case . ordinal, . spellOut, . currencyPlural:
196
198
return 0
197
199
198
- case . currency, . currencyISOCode, . currencyAccounting, . decimal, . percent:
200
+ case . none , . currency, . currencyISOCode, . currencyAccounting, . decimal, . percent, . scientific :
199
201
return 1
200
202
}
201
203
}
@@ -245,14 +247,14 @@ open class NumberFormatter : Formatter {
245
247
return 0
246
248
247
249
case . currency, . none, . currencyISOCode, . currencyAccounting, . decimal, . percent, . scientific:
248
- return 1
250
+ return - 1
249
251
}
250
252
}
251
253
252
254
private func defaultMaximumSignificantDigits( ) -> Int {
253
255
switch numberStyle {
254
256
case . none, . currency, . currencyISOCode, . currencyAccounting, . decimal, . percent, . scientific:
255
- return 6
257
+ return - 1
256
258
257
259
case . ordinal, . spellOut, . currencyPlural:
258
260
return 0
@@ -286,6 +288,16 @@ open class NumberFormatter : Formatter {
286
288
}
287
289
}
288
290
291
+ private func defaultFormatWidth( ) -> Int {
292
+ switch numberStyle {
293
+ case . ordinal, . ordinal, . spellOut, . currencyPlural:
294
+ return 0
295
+
296
+ case . none, . decimal, . currency, . percent, . scientific, . currencyISOCode, . currencyAccounting:
297
+ return - 1
298
+ }
299
+ }
300
+
289
301
private var _numberStyle : Style = . none
290
302
open var numberStyle : Style {
291
303
get {
@@ -683,10 +695,10 @@ open class NumberFormatter : Formatter {
683
695
}
684
696
}
685
697
686
- private var _formatWidth : Int = 0
698
+ private var _formatWidth : Int ?
687
699
open var formatWidth : Int {
688
700
get {
689
- return _formatWidth
701
+ return _formatWidth ?? defaultFormatWidth ( )
690
702
}
691
703
set {
692
704
_reset ( )
@@ -849,6 +861,9 @@ open class NumberFormatter : Formatter {
849
861
_reset ( )
850
862
_usesSignificantDigits = true
851
863
_minimumSignificantDigits = newValue
864
+ if _maximumSignificantDigits == nil && newValue > defaultMinimumSignificantDigits ( ) {
865
+ _maximumSignificantDigits = ( newValue < 1000 ) ? 999 : newValue
866
+ }
852
867
}
853
868
}
854
869
0 commit comments