Last active
August 29, 2015 14:23
-
-
Save joemasilotti/16d52648469d7d20d6a5 to your computer and use it in GitHub Desktop.
How to format ABV% in Objective-C.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface ABVFormatter : NSObject | |
- (NSString *)formattedABV:(NSNumber *)abv; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ABVFormatter.h" | |
@interface ABVFormatter () | |
@property (nonatomic) NSNumberFormatter *formatter; | |
@end | |
@implementation ABVFormatter | |
- (NSString *)formattedABV:(NSNumber *)abv { | |
self.formatter = [[NSNumberFormatter alloc] init]; | |
self.formatter.maximumFractionDigits = 2; | |
NSString *abvString = [self.formatter stringFromNumber:abv]; | |
return [abvString stringByAppendingString:@"%"]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "SpecHelper.h" | |
using namespace Cedar::Matchers; | |
using namespace Cedar::Doubles; | |
SPEC_BEGIN(ABVFormatterSpec) | |
describe(@"ABVFormatter", ^{ | |
__block ABVFormatter *subject; | |
beforeEach(^{ | |
subject = [[ABVFormatter alloc] init]; | |
}); | |
describe(@"formatting an ABVr", ^{ | |
__block NSNumber *abv; | |
__block NSString *formattedABV; | |
subjectAction(^{ | |
formattedABV = [subject formattedABV:abv]; | |
}); | |
context(@"with an ABV with one decimal place", ^{ | |
beforeEach(^{ | |
abv = @5.3; | |
}); | |
it(@"should only display one decimal place", ^{ | |
formattedABV should equal(@"5.3%"); | |
}); | |
}); | |
context(@"with an ABV with three decimal places", ^{ | |
beforeEach(^{ | |
abv = @5.321; | |
}); | |
it(@"should only display the first two decimal places", ^{ | |
formattedABV should equal(@"5.32%"); | |
}); | |
}); | |
context(@"with an ABV 10% or higher", ^{ | |
beforeEach(^{ | |
abv = @15.32; | |
}); | |
it(@"should display both digits", ^{ | |
formattedABV should equal(@"15.32%"); | |
}); | |
}); | |
}); | |
}); | |
SPEC_END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment