-
-
Save Ricardo1980/4730015 to your computer and use it in GitHub Desktop.
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
// | |
// NSString+Distance.h | |
// | |
// Created by Alexander Johansson on 2011-11-27. | |
// Based on http://stackoverflow.com/q/5684973/590396 | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSString (Distance) | |
+ (NSString *)stringWithDistance:(double)distance; | |
// Return a string of the number to one decimal place and with commas & periods based on the locale. | |
+ (NSString *)stringWithDouble:(double)value; | |
@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
// | |
// NSString+Distance.m | |
// | |
// Created by Alexander Johansson on 2011-11-27. | |
// Based on http://stackoverflow.com/q/5684973/590396 | |
// | |
#import "NSString+Distance.h" | |
#define METERS_TO_FEET 3.2808399 | |
#define METERS_TO_MILES 0.000621371192 | |
#define METERS_CUTOFF 1000 | |
#define FEET_CUTOFF 3281 | |
#define FEET_IN_MILES 5280 | |
@implementation NSString (Distance) | |
+ (NSString *)stringWithDistance:(double)distance { | |
BOOL isMetric = [[[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem] boolValue]; | |
NSString *format; | |
if (isMetric) { | |
if (distance < METERS_CUTOFF) { | |
format = _(@"%@ metres"); | |
} else { | |
format = _(@"%@ km"); | |
distance = distance / 1000; | |
} | |
} else { // assume Imperial / U.S. | |
distance = distance * METERS_TO_FEET; | |
if (distance < FEET_CUTOFF) { | |
format = _(@"%@ feet"); | |
} else { | |
format = _(@"%@ miles"); | |
distance = distance / FEET_IN_MILES; | |
} | |
} | |
return [NSString stringWithFormat:format, [self stringWithDouble:distance]]; | |
} | |
// Return a string of the number to one decimal place and with commas & periods based on the locale. | |
+ (NSString *)stringWithDouble:(double)value { | |
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; | |
[numberFormatter setLocale:[NSLocale currentLocale]]; | |
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; | |
[numberFormatter setMaximumFractionDigits:1]; | |
return [numberFormatter stringFromNumber:[NSNumber numberWithDouble:value]]; | |
} | |
@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
// | |
// NSString+Distance.h | |
// | |
// Created by Alexander Johansson on 2011-11-27. | |
// Based on http://stackoverflow.com/q/5684973/590396 | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSString (Distance) | |
+ (NSString *)stringWithDistance:(double)distance; | |
// Return a string of the number to one decimal place and with commas & periods based on the locale. | |
+ (NSString *)stringWithDouble:(double)value; | |
@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
// | |
// NSString+Distance.m | |
// | |
// Created by Alexander Johansson on 2011-11-27. | |
// Based on http://stackoverflow.com/q/5684973/590396 | |
// | |
#import "NSString+Distance.h" | |
#define METERS_TO_FEET 3.2808399 | |
#define METERS_TO_MILES 0.000621371192 | |
#define METERS_CUTOFF 1000 | |
#define FEET_CUTOFF 3281 | |
#define FEET_IN_MILES 5280 | |
@implementation NSString (Distance) | |
+ (NSString *)stringWithDistance:(double)distance { | |
BOOL isMetric = [[[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem] boolValue]; | |
NSString *format; | |
if (isMetric) { | |
if (distance < METERS_CUTOFF) { | |
format = _(@"%@ metres"); | |
} else { | |
format = _(@"%@ km"); | |
distance = distance / 1000; | |
} | |
} else { // assume Imperial / U.S. | |
distance = distance * METERS_TO_FEET; | |
if (distance < FEET_CUTOFF) { | |
format = _(@"%@ feet"); | |
} else { | |
format = _(@"%@ miles"); | |
distance = distance / FEET_IN_MILES; | |
} | |
} | |
return [NSString stringWithFormat:format, [self stringWithDouble:distance]]; | |
} | |
// Return a string of the number to one decimal place and with commas & periods based on the locale. | |
+ (NSString *)stringWithDouble:(double)value { | |
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; | |
[numberFormatter setLocale:[NSLocale currentLocale]]; | |
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; | |
[numberFormatter setMaximumFractionDigits:1]; | |
return [numberFormatter stringFromNumber:[NSNumber numberWithDouble:value]]; | |
} | |
@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
// [..] all your stuff | |
#define _(localizedString) NSLocalizedString(localizedString, nil) // Preprocessor macro. Shortcut to translate strings. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment