Skip to content

Instantly share code, notes, and snippets.

@joemasilotti
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemasilotti/16d52648469d7d20d6a5 to your computer and use it in GitHub Desktop.
Save joemasilotti/16d52648469d7d20d6a5 to your computer and use it in GitHub Desktop.
How to format ABV% in Objective-C.
#import <Foundation/Foundation.h>
@interface ABVFormatter : NSObject
- (NSString *)formattedABV:(NSNumber *)abv;
@end
#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
#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