Skip to content

Instantly share code, notes, and snippets.

@Ricardo1980
Forked from KATT/NSString+Distance.h
Created February 7, 2013 10:03
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 Ricardo1980/4730015 to your computer and use it in GitHub Desktop.
Save Ricardo1980/4730015 to your computer and use it in GitHub Desktop.
//
// 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
//
// 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
//
// 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
//
// 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
// [..] 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