Skip to content

Commit b914426

Browse files
authored
Merge pull request #2062 from spevans/pr_sr_10245
2 parents c1a1520 + 0dceb78 commit b914426

File tree

2 files changed

+260
-33
lines changed

2 files changed

+260
-33
lines changed

Foundation/NumberFormatter.swift

Lines changed: 75 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ open class NumberFormatter : Formatter {
5858

5959
let obj = CFNumberFormatterCreate(kCFAllocatorSystemDefault, locale._cfObject, numberStyle)!
6060
_setFormatterAttributes(obj)
61-
if let format = _format {
61+
if _positiveFormat != nil || _negativeFormat != nil {
62+
var format = _positiveFormat ?? "#"
63+
if let negative = _negativeFormat {
64+
format.append(";")
65+
format.append(negative)
66+
}
6267
CFNumberFormatterSetFormat(obj, format._cfObject)
6368
}
6469
_currentCfFormatter = obj
@@ -255,17 +260,6 @@ open class NumberFormatter : Formatter {
255260
}
256261
}
257262

258-
private var _negativeFormat: String!
259-
open var negativeFormat: String! {
260-
get {
261-
return _negativeFormat
262-
}
263-
set {
264-
_reset()
265-
_negativeFormat = newValue
266-
}
267-
}
268-
269263
private var _textAttributesForNegativeValues: [String : Any]?
270264
open var textAttributesForNegativeValues: [String : Any]? {
271265
get {
@@ -277,17 +271,6 @@ open class NumberFormatter : Formatter {
277271
}
278272
}
279273

280-
private var _positiveFormat: String!
281-
open var positiveFormat: String! {
282-
get {
283-
return _positiveFormat
284-
}
285-
set {
286-
_reset()
287-
_positiveFormat = newValue
288-
}
289-
}
290-
291274
private var _textAttributesForPositiveValues: [String : Any]?
292275
open var textAttributesForPositiveValues: [String : Any]? {
293276
get {
@@ -368,7 +351,7 @@ open class NumberFormatter : Formatter {
368351
private var _zeroSymbol: String?
369352
open var zeroSymbol: String? {
370353
get {
371-
return _zeroSymbol ?? _getFormatterAttribute(_cfFormatter, attributeName: kCFNumberFormatterZeroSymbol)
354+
return _zeroSymbol
372355
}
373356
set {
374357
_reset()
@@ -865,17 +848,80 @@ open class NumberFormatter : Formatter {
865848
}
866849
}
867850

868-
private var _format: String?
851+
private func getFormatterComponents() -> (String, String) {
852+
let format = CFNumberFormatterGetFormat(_cfFormatter)._swiftObject
853+
let components = format.components(separatedBy: ";")
854+
let positive = _positiveFormat ?? components.first ?? "#"
855+
let negative = _negativeFormat ?? components.last ?? "#"
856+
return (positive, negative)
857+
}
858+
859+
private func getZeroFormat() -> String {
860+
return string(from: 0) ?? "0"
861+
}
862+
869863
open var format: String {
870864
get {
871-
if _format == nil {
872-
_format = CFNumberFormatterGetFormat(_cfFormatter)._swiftObject
865+
let (p, n) = getFormatterComponents()
866+
let z = _zeroSymbol ?? getZeroFormat()
867+
return "\(p);\(z);\(n)"
868+
}
869+
set {
870+
// Special case empty string
871+
if newValue == "" {
872+
_positiveFormat = ""
873+
_negativeFormat = "-"
874+
_zeroSymbol = "0"
875+
_reset()
876+
} else {
877+
let components = newValue.components(separatedBy: ";")
878+
let count = components.count
879+
guard count <= 3 else { return }
880+
_reset()
881+
882+
_positiveFormat = components.first ?? ""
883+
if count == 1 {
884+
_negativeFormat = "-\(_positiveFormat ?? "")"
885+
}
886+
else if count == 2 {
887+
_negativeFormat = components[1]
888+
_zeroSymbol = getZeroFormat()
889+
}
890+
else if count == 3 {
891+
_zeroSymbol = components[1]
892+
_negativeFormat = components[2]
893+
}
894+
895+
if _negativeFormat == nil {
896+
_negativeFormat = getFormatterComponents().1
897+
}
898+
899+
if _zeroSymbol == nil {
900+
_zeroSymbol = getZeroFormat()
901+
}
873902
}
874-
return _format ?? "#"
903+
}
904+
}
905+
906+
private var _positiveFormat: String!
907+
open var positiveFormat: String! {
908+
get {
909+
return getFormatterComponents().0
910+
}
911+
set {
912+
_reset()
913+
_positiveFormat = newValue
914+
}
915+
}
916+
917+
private var _negativeFormat: String!
918+
open var negativeFormat: String! {
919+
get {
920+
return getFormatterComponents().1
875921
}
876922
set {
877923
_reset()
878-
_format = newValue
924+
_negativeFormat = newValue
879925
}
880926
}
881927

0 commit comments

Comments
 (0)