88
99import Foundation
1010
11- public struct RoundedDecimal < T: DecimalPlaces > :
12- Codable , ExpressibleByIntegerLiteral , ExpressibleByStringLiteral ,
13- CustomStringConvertible , CustomDebugStringConvertible {
14-
15- public typealias IntegerLiteralType = Int
16- public typealias StringLiteralType = String
11+ public struct RoundedDecimal < T: DecimalPlaces > {
1712
1813 private let underlyingValue : Decimal
1914
20- // MARK: ExpressibleByStringLiteral implementation
21-
22- public init ( stringLiteral: String ) {
23-
24- let decimalValue = Decimal ( string: stringLiteral) ?? . nan
25-
26- self . init ( value: decimalValue)
27- }
28-
29- // MARK: ExpressibleByIntegerLiteral implementation
30-
31- public init ( integerLiteral value: IntegerLiteralType ) {
32-
33- self . init ( value: Decimal ( value) )
34- }
35-
36- // MARK: Decodable implementation
37-
38- public init ( from decoder: Decoder ) throws {
39-
40- let container = try decoder. singleValueContainer ( )
41-
42- let stringRepresentation = try container. decode ( String . self)
43-
44- self . init ( stringLiteral: stringRepresentation)
45- }
46-
47- public func encode( to encoder: Encoder ) throws {
48-
49- var container = encoder. singleValueContainer ( )
50-
51- try container. encode ( description)
52- }
53-
5415 private init ( value: Decimal ) {
5516
5617 let decimalHandler = NSDecimalNumberHandler ( roundingMode: . bankers,
@@ -98,9 +59,7 @@ public struct RoundedDecimal<T: DecimalPlaces>:
9859 return RoundedDecimal ( value: . nan)
9960 }
10061
101- // MARK: CustomStringConvertible, CustomDebugStringConvertible implementation
102-
103- private let descriptionFormatter = { ( ) -> NumberFormatter in
62+ private let formatter = { ( ) -> NumberFormatter in
10463
10564 let formatter = NumberFormatter ( )
10665
@@ -110,21 +69,61 @@ public struct RoundedDecimal<T: DecimalPlaces>:
11069
11170 return formatter
11271 } ( )
72+ }
73+
74+ extension RoundedDecimal : ExpressibleByStringLiteral {
11375
114- public var description : String {
76+ public init ( stringLiteral : String ) {
11577
116- return descriptionFormatter. string ( from: underlyingValue as NSDecimalNumber ) !
78+ let decimalValue = Decimal ( string: stringLiteral) ?? . nan
79+
80+ self . init ( value: decimalValue)
11781 }
82+ }
83+
84+ extension RoundedDecimal : ExpressibleByIntegerLiteral {
11885
119- public var debugDescription : String {
86+ public init ( integerLiteral value : IntegerLiteralType ) {
12087
121- return description
88+ self . init ( value : Decimal ( value ) )
12289 }
90+ }
91+
92+ extension RoundedDecimal : Codable {
12393
12494 enum CodingKeys : String , CodingKey {
12595
12696 case underlyingValue
12797 }
98+
99+ public init ( from decoder: Decoder ) throws {
100+
101+ let container = try decoder. singleValueContainer ( )
102+
103+ let stringRepresentation = try container. decode ( String . self)
104+
105+ self . init ( stringLiteral: stringRepresentation)
106+ }
107+
108+ public func encode( to encoder: Encoder ) throws {
109+
110+ var container = encoder. singleValueContainer ( )
111+
112+ try container. encode ( description)
113+ }
114+ }
115+
116+ extension RoundedDecimal : CustomStringConvertible , CustomDebugStringConvertible {
117+
118+ public var description : String {
119+
120+ return formatter. string ( from: underlyingValue as NSDecimalNumber ) !
121+ }
122+
123+ public var debugDescription : String {
124+
125+ return description
126+ }
128127}
129128
130129extension RoundedDecimal : Equatable , Comparable {
0 commit comments