Permalink
Browse files

Use NSDecimalNumber for amount.

  • Loading branch information...
1 parent 4253508 commit 860c434e8e45c88bacd19235dfb3a51e60bfb674 @ma11hew28 ma11hew28 committed Jun 29, 2012
View
@@ -9,9 +9,9 @@ typedef enum {
@property (copy, nonatomic) NSString *transactionID;
@property (nonatomic) VenmoTransactionType type;
-@property (copy, nonatomic) NSString *fromUserID; // set on completion
-@property (copy, nonatomic) NSString *toUserID; // set on completion
-@property (nonatomic) CGFloat amount;
+@property (copy, nonatomic) NSString *fromUserID;
+@property (copy, nonatomic) NSString *toUserID;
+@property (strong, nonatomic) NSDecimalNumber *amount;
@property (copy, nonatomic) NSString *note;
@property (copy, nonatomic) NSString *toUserHandle; // cell number, email, @twitter, Venmo username
@property (nonatomic) BOOL success;
View
@@ -4,7 +4,9 @@
#import "VenmoTransaction.h"
#import "VenmoTransaction_Internal.h"
-@implementation VenmoTransaction
+@implementation VenmoTransaction {
+ NSNumberFormatter *amountNumberFormatter;
+}
@synthesize transactionID;
@synthesize type;
@@ -29,7 +31,15 @@ - (NSString *)typeStringPast {
}
- (NSString *)amountString {
- return [NSString stringWithFormat:@"%.2f", amount];
+ // TODO: Consider making amountNumberFormatter a static variable and adding another static
+ // variable called transactionCount, incremented in init & decremented in dealloc.
+ // Then, in dealloc, if transactionCount == 0, set amountNumberFormatter = nil.
+ if (!amountNumberFormatter) {
+ amountNumberFormatter = [[NSNumberFormatter alloc] init];
+ [amountNumberFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
+ [amountNumberFormatter setMinimumFractionDigits:2];
+ }
+ return [amountNumberFormatter stringFromNumber:amount];
}
#pragma mark - Internal
@@ -55,7 +65,8 @@ + (id)transactionWithDictionary:(NSDictionary *)dictionary {
transaction.type = [VenmoTransaction typeWithString:[dictionary objectForKey:@"verb"]];
transaction.fromUserID = [dictionary stringForKey:@"actor_user_id"];
transaction.toUserID = [dictionary stringForKey:@"target_user_id"];
- transaction.amount = [[dictionary objectOrNilForKey:@"amount"] floatValue];
+ transaction.amount = [NSDecimalNumber decimalNumberWithString:
+ [dictionary stringForKey:@"amount"]];
transaction.note = [dictionary stringForKey:@"note"];
transaction.success = [dictionary boolForKey:@"success"];
return transaction;
@@ -27,7 +27,7 @@ - (void)viewDidLoad {
venmoTransaction = [[VenmoTransaction alloc] init];
venmoTransaction.type = VenmoTransactionTypePay;
- venmoTransaction.amount = 3.45f;
+ venmoTransaction.amount = [NSDecimalNumber decimalNumberWithString:@"3.45"];
venmoTransaction.note = @"hello world";
venmoTransaction.toUserHandle = @"mattdipasquale";
@@ -21,7 +21,7 @@
[[UIDevice currentDevice] uniqueIdentifier]];
transaction = [[VenmoTransaction alloc] init];
transaction.type = VenmoTransactionTypePay;
- transaction.amount = 3.45f;
+ transaction.amount = [NSDecimalNumber decimalNumberWithString:@"3.45"];
transaction.note = @"hello world";
transaction.toUserHandle = @"shreyanstest";
});
@@ -12,7 +12,7 @@
transaction = [[VenmoTransaction alloc] init];
transaction.transactionID = @"1234";
transaction.type = VenmoTransactionTypePay;
- transaction.amount = 3.45f;
+ transaction.amount = [NSDecimalNumber decimalNumberWithString:@"3.45"];
transaction.note = @"hello world";
transaction.toUserHandle = @"shreyanstest";
});
@@ -23,7 +23,7 @@
specify(^{ [[transaction.transactionID should] equal:@"1234"]; });
specify(^{ [[theValue(transaction.type) should]
equal:theValue(VenmoTransactionTypePay)]; });
- specify(^{ [[theValue(transaction.amount) should] equal:theValue(3.45f)]; });
+ specify(^{ [[transaction.amount should] equal:[NSDecimalNumber decimalNumberWithString:@"3.45"]]; });
specify(^{ [[transaction.note should] equal:@"hello world"]; });
specify(^{ [[transaction.toUserHandle should] equal:@"shreyanstest"]; });
});

0 comments on commit 860c434

Please sign in to comment.