-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWXMLNumberMapping.m
53 lines (44 loc) · 1.76 KB
/
SWXMLNumberMapping.m
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
//
// SWXMLNumberMapping.m
// This file is part of the "SWXMLMapping" project, and is distributed under the MIT License.
//
// Created by Samuel Williams on 14/11/05.
// Copyright 2005 Samuel Williams. All rights reserved.
//
#import "SWXMLNumberMapping.h"
#import "SWXMLMapping.h"
@implementation SWXMLNumberMapping
- serializedObjectMember:(id)object withMapping:(SWXMLMapping*)mapping {
NSNumberFormatter * numberFormatter = nil;
NSNumber * number = [object valueForKeyPath:self.keyPath];
if (number) {
numberFormatter = [NSNumberFormatter new];
NSString * currencyCode = (NSString *)mapping.metadata[@"currencyCode"];
if (currencyCode) {
numberFormatter.currencyCode = currencyCode;
}
NSString *format;
format = [self.attributes valueForKey:@"format"];
if ([format isEqualToString:@"currency"]) {
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
//[numberFormatter setFormat:@"$#,###.00;0.00;($#,##0.00)"];
} else if ([format isEqualToString:@"decimal"]) {
//[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
numberFormatter.format = @"###0.00;0;-###0.00";
} else if ([format isEqualToString:@"percent"]) {
numberFormatter.numberStyle = NSNumberFormatterPercentStyle;
} else if ([format isEqualToString:@"scientific"]) {
numberFormatter.numberStyle = NSNumberFormatterScientificStyle;
} else if ([format isEqualToString:@"integer"]) {
numberFormatter.format = @"##";
} else if (format != nil) {
numberFormatter.format = format;
}
NSString * formattedNumber = [numberFormatter stringFromNumber:number];
NSDictionary * attributes = @{@"value": number.stringValue};
return [SWXMLTags tagNamed:self.tag forCDATA:formattedNumber withAttributes:attributes];
} else {
return nil;
}
}
@end