-
Notifications
You must be signed in to change notification settings - Fork 333
/
CPNumberFormatter.j
267 lines (226 loc) · 9.71 KB
/
CPNumberFormatter.j
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* CPNumberFormatter.j
* Foundation
*
* Created by Alexander Ljungberg.
* Copyright 2011, WireLoad Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#import "Ref.h"
@import <Foundation/CPString.j>
@import <Foundation/CPFormatter.j>
@import <Foundation/CPDecimalNumber.j>
#define UPDATE_NUMBER_HANDLER_IF_NECESSARY() if (!_numberHandler) \
_numberHandler = [CPDecimalNumberHandler decimalNumberHandlerWithRoundingMode:_roundingMode scale:_maximumFractionDigits raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
#define SET_NEEDS_NUMBER_HANDLER_UPDATE() _numberHandler = nil;
CPNumberFormatterNoStyle = 0;
CPNumberFormatterDecimalStyle = 1;
CPNumberFormatterCurrencyStyle = 2;
CPNumberFormatterPercentStyle = 3;
CPNumberFormatterScientificStyle = 4;
CPNumberFormatterSpellOutStyle = 5;
CPNumberFormatterRoundCeiling = CPRoundUp;
CPNumberFormatterRoundFloor = CPRoundDown;
CPNumberFormatterRoundDown = CPRoundDown;
CPNumberFormatterRoundUp = CPRoundUp;
CPNumberFormatterRoundHalfEven = CPRoundBankers;
CPNumberFormatterRoundHalfDown = _CPRoundHalfDown;
CPNumberFormatterRoundHalfUp = CPRoundPlain;
/*!
@ingroup foundation
@class CPNumberFormatter
CPNumberFormatter takes a numeric CPNumber value and formats it as text for
display. It also supports the converse, taking text and interpreting it as a
CPNumber by configurable formatting rules.
*/
@implementation CPNumberFormatter : CPFormatter
{
CPNumberFormatterStyle _numberStyle @accessors(property=numberStyle);
CPString _perMillSymbol @accessors(property=perMillSymbol);
CPString _groupingSeparator @accessors(property=groupingSeparator);
CPNumberFormatterRoundingMode _roundingMode @accessors(property=roundingMode);
CPUInteger _minimumFractionDigits @accessors(property=minimumFractionDigits);
CPUInteger _maximumFractionDigits @accessors(property=maximumFractionDigits);
CPString _currencyCode @accessors(property=currencyCode);
CPString _currencySymbol @accessors(property=currencySymbol);
BOOL _generatesDecimalNumbers @accessors(property=generatesDecimalNumbers);
// Note that we do not implement the 10.0 style number formatter, but the 10.4+ formatter. Therefore
// we don't expose this through a `roundingBehavior` property.
CPDecimalNumberHandler _numberHandler;
}
- (id)init
{
if (self = [super init])
{
_roundingMode = CPNumberFormatterRoundHalfEven;
_minimumFractionDigits = 0;
_maximumFractionDigits = 0;
_groupingSeparator = @",";
_generatesDecimalNumbers = YES;
// FIXME Add locale support.
_currencyCode = @"USD";
_currencySymbol = @"$";
}
return self;
}
- (CPString)stringFromNumber:(CPNumber)number
{
if (_numberStyle == CPNumberFormatterPercentStyle)
{
number *= 100.0;
}
var dcmn = [number isKindOfClass:CPDecimalNumber] ? number : [[CPDecimalNumber alloc] _initWithJSNumber:number];
// TODO Add locale support.
switch (_numberStyle)
{
case CPNumberFormatterCurrencyStyle:
case CPNumberFormatterDecimalStyle:
case CPNumberFormatterPercentStyle:
UPDATE_NUMBER_HANDLER_IF_NECESSARY();
dcmn = [dcmn decimalNumberByRoundingAccordingToBehavior:_numberHandler];
var output = [dcmn descriptionWithLocale:nil],
parts = [output componentsSeparatedByString:"."], // FIXME Locale specific.
preFraction = parts[0],
fraction = parts.length > 1 ? parts[1] : "",
preFractionLength = [preFraction length],
commaPosition = 3;
while (fraction.length < _minimumFractionDigits)
fraction += "0";
// TODO This is just a temporary solution. Should be generalised.
// Add in thousands separators.
if (_groupingSeparator)
{
for (var commaPosition = 3, prefLength = [preFraction length]; commaPosition < prefLength; commaPosition += 4)
{
preFraction = [preFraction stringByReplacingCharactersInRange:CPMakeRange(prefLength - commaPosition, 0) withString:_groupingSeparator];
prefLength += 1;
}
}
var string = preFraction;
if (fraction)
string += "." + fraction;
if (_numberStyle === CPNumberFormatterCurrencyStyle)
{
if (_currencySymbol)
string = _currencySymbol + string;
else
string = _currencyCode + string;
}
if (_numberStyle == CPNumberFormatterPercentStyle)
{
string += "%";
}
return string;
default:
return [number description];
}
}
- (CPNumber)numberFromString:(CPString)aString
{
if (_generatesDecimalNumbers)
return [CPDecimalNumber decimalNumberWithString:aString];
else
return parseFloat(aString);
}
- (CPString)stringForObjectValue:(id)anObject
{
if ([anObject isKindOfClass:[CPNumber class]])
return [self stringFromNumber:anObject];
else
return [anObject description];
}
- (CPString)editingStringForObjectValue:(id)anObject
{
return [self stringForObjectValue:anObject];
}
- (BOOL)getObjectValue:(id)anObject forString:(CPString)aString errorDescription:(CPString)anError
{
// TODO Error handling.
var value = [self numberFromString:aString];
AT_DEREF(anObject, value);
return YES;
}
- (void)setNumberStyle:(CPNumberFormatterStyle)aStyle
{
_numberStyle = aStyle;
switch (aStyle)
{
case CPNumberFormatterDecimalStyle:
_minimumFractionDigits = 0;
_maximumFractionDigits = 3;
SET_NEEDS_NUMBER_HANDLER_UPDATE();
break;
case CPNumberFormatterCurrencyStyle:
_minimumFractionDigits = 2;
_maximumFractionDigits = 2;
SET_NEEDS_NUMBER_HANDLER_UPDATE();
break;
}
}
- (void)setRoundingMode:(CPNumberFormatterRoundingMode)aRoundingMode
{
_roundingMode = aRoundingMode;
SET_NEEDS_NUMBER_HANDLER_UPDATE();
}
- (void)setMinimumFractionDigits:(CPUInteger)aNumber
{
_minimumFractionDigits = aNumber;
SET_NEEDS_NUMBER_HANDLER_UPDATE();
}
- (void)setMaximumFractionDigits:(CPUInteger)aNumber
{
_maximumFractionDigits = aNumber;
SET_NEEDS_NUMBER_HANDLER_UPDATE();
}
@end
var CPNumberFormatterStyleKey = "CPNumberFormatterStyleKey",
CPNumberFormatterMinimumFractionDigitsKey = @"CPNumberFormatterMinimumFractionDigitsKey",
CPNumberFormatterMaximumFractionDigitsKey = @"CPNumberFormatterMaximumFractionDigitsKey",
CPNumberFormatterRoundingModeKey = @"CPNumberFormatterRoundingModeKey",
CPNumberFormatterGroupingSeparatorKey = @"CPNumberFormatterGroupingSeparatorKey",
CPNumberFormatterCurrencyCodeKey = @"CPNumberFormatterCurrencyCodeKey",
CPNumberFormatterCurrencySymbolKey = @"CPNumberFormatterCurrencySymbolKey",
CPNumberFormatterGeneratesDecimalNumbers = @"CPNumberFormatterGeneratesDecimalNumbers";
@implementation CPNumberFormatter (CPCoding)
- (id)initWithCoder:(CPCoder)aCoder
{
self = [super initWithCoder:aCoder];
if (self)
{
_numberStyle = [aCoder decodeIntForKey:CPNumberFormatterStyleKey];
_minimumFractionDigits = [aCoder decodeIntForKey:CPNumberFormatterMinimumFractionDigitsKey];
_maximumFractionDigits = [aCoder decodeIntForKey:CPNumberFormatterMaximumFractionDigitsKey];
_roundingMode = [aCoder decodeIntForKey:CPNumberFormatterRoundingModeKey];
_groupingSeparator = [aCoder decodeObjectForKey:CPNumberFormatterGroupingSeparatorKey];
_currencyCode = [aCoder decodeObjectForKey:CPNumberFormatterCurrencyCodeKey];
_currencySymbol = [aCoder decodeObjectForKey:CPNumberFormatterCurrencySymbolKey];
_generatesDecimalNumbers = [aCoder decodeBoolForKey:CPNumberFormatterGeneratesDecimalNumbers];
}
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
[super encodeWithCoder:aCoder];
[aCoder encodeInt:_numberStyle forKey:CPNumberFormatterStyleKey];
[aCoder encodeInt:_minimumFractionDigits forKey:CPNumberFormatterMinimumFractionDigitsKey];
[aCoder encodeInt:_maximumFractionDigits forKey:CPNumberFormatterMaximumFractionDigitsKey];
[aCoder encodeInt:_roundingMode forKey:CPNumberFormatterRoundingModeKey];
[aCoder encodeObject:_groupingSeparator forKey:CPNumberFormatterGroupingSeparatorKey];
[aCoder encodeObject:_currencyCode forKey:CPNumberFormatterCurrencyCodeKey];
[aCoder encodeObject:_currencySymbol forKey:CPNumberFormatterCurrencySymbolKey];
[aCoder encodeBool:_generatesDecimalNumbers forKey:CPNumberFormatterGeneratesDecimalNumbers];
}
@end