2010-02-24 13 views
10

मेरे पास एक फ्लोट के रूप में दूरी है और मैं इसे मानव पाठकों के लिए अच्छी तरह प्रारूपित करने का एक तरीका ढूंढ रहा हूं। आदर्श रूप से, मैं इसे एम से किमी में बदलना चाहता हूं क्योंकि यह बड़ा हो जाता है, और अच्छी तरह से संख्या को गोल करने के लिए। मील में कनवर्ट करना एक बोनस होगा। मुझे यकीन है कि बहुत से लोगों को इनमें से किसी एक की आवश्यकता है और मुझे आशा है कि कहीं भी कुछ कोड फ़्लोटिंग हो रहा है।दूरी के लिए उद्देश्य सी स्ट्रिंग फॉर्मेटर

यहां बताया गया है मैं प्रारूपों चाहते हैं:

  • 0-100m: 47M (एक पूरी संख्या के रूप में)
  • 100-1000m: 325m या 320M (निकटतम 5 या 10 मीटर की दूरी पर गोल)
  • 1000-10000m: 1.2km (एक दशमलव स्थान के साथ निकटतम दौर)
  • 10000m +: 21km

अगर कोई कोड उपलब्ध है, कैसे मैं अपने खुद के फ़ॉर्मेटर लिख सकते हैं ?

धन्यवाद

उत्तर

24

इन समाधानों में से कोई भी वास्तव में मुलाकात की है कि मैं क्या की तलाश में था, इसलिए मैं उन पर बनाया: के लिए

#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 

- (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]]; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    double distance = 5434.45; 
    NSLog(@"%f meters is %@", distance, [self stringWithDistance:distance]); 

    distance = 543.45; 
    NSLog(@"%f meters is %@", distance, [self stringWithDistance:distance]);  

    distance = 234234.45; 
    NSLog(@"%f meters is %@", distance, [self stringWithDistance:distance]);  
} 
+3

मुझे यह समाधान पसंद आया। इसके आधार पर 'एनएसएसटींग + दूरी' श्रेणी बनाई गई। गिस्ट: https://gist.github.com/1397837 – Alex

+0

धन्यवाद यह है कि मुझे CLLocationDistance की आवश्यकता है। –

+0

इस श्रेणी के साथ कौन से लाइसेंस/शर्तें जुड़े हैं। मैं इसे अपने ऐप में इस्तेमाल करने की योजना बना रहा हूं। मेरे लाइसेंस वीसी में, क्या आपको मध्यस्थता पसंद आएगी? – Andrew

17

हाँ आप अपने स्वयं के फ़ॉर्मेटर लिखने के लिए की तरह

#include <math.h> 
NSString* convertDistanceToString(float distance) { 
    if (distance < 100) 
     return [NSString stringWithFormat:@"%g m", roundf(distance)]; 
    else if (distance < 1000) 
     return [NSString stringWithFormat:@"%g m", roundf(distance/5)*5]; 
    else if (distance < 10000) 
     return [NSString stringWithFormat:@"%g km", roundf(distance/100)/10]; 
    else 
     return [NSString stringWithFormat:@"%g km", roundf(distance/1000)]; 
} 
... 
NSLog(@"The distance is %@", convertDistanceToString(1024)); 
+0

उत्तर के लिए बहुत बहुत धन्यवाद, यह मेरी आवश्यकता के लिए बिल्कुल सही था (और मुझे बहुत समय बचाया गया)। –

2

जरूरत है यहाँ कैसे मैं यह कर रहा है। यह स्ट्रिंग को उचित रूप से प्रारूपित करने के लिए उपयोगकर्ता के लोकेल का उपयोग करता है, जिसे आपको शायद भी करना चाहिए।

// Returns a string representing the distance in the correct units. 
// If distance is greater than convert max in feet or meters, 
// the distance in miles or km is returned instead 
NSString* getDistString(float distance, int convertMax, BOOL includeUnit) { 
    NSString * unitName; 
    if (METRIC) { 
    unitName = @"m"; 
    if (convertMax != -1 && distance > convertMax) { 
     unitName = @"km"; 
     distance = distance/1000; 
    } 
    } else { 
    unitName = @"ft"; 
    if (convertMax != -1 && distance > convertMax) { 
     unitName = @"mi"; 
     distance = distance/5280; 
    } 
    distance = metersToFeet(distance); 
    } 

    if (includeUnit) return [NSString stringWithFormat:@"%@ %@", formatDecimal_1(distance), unitName]; 

    return formatDecimal_1(distance); 

} 
// returns a string if the number with one decimal place of precision 
// sets the style (commas or periods) based on the locale 
NSString * formatDecimal_1(float num) { 
    static NSNumberFormatter *numFormatter; 
    if (!numFormatter) { 
    numFormatter = [[[NSNumberFormatter alloc] init] retain]; 
    [numFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
    [numFormatter setLocale:[NSLocale currentLocale]]; 
    [numFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; 
    [numFormatter setMaximumFractionDigits:1]; 
    [numFormatter setMinimumFractionDigits:1]; 
    } 

    return [numFormatter stringFromNumber:F(num)]; 

} 
+0

बहुत बहुत धन्यवाद। मैंने संख्या प्रारूपों के लिए स्थानीयकरण का उपयोग करने के बारे में सोचा नहीं था।मुझे इसे इस्तेमाल किए गए कोड में जोड़ने के लिए समय लेना होगा। –

+0

आप कैसे जानते हैं कि उपयोगकर्ता मेट्रिक चाहता है या नहीं? – ma11hew28

+0

यदि वे अमेरिका में नहीं रहते हैं, तो हम मीट्रिक के लिए डिफ़ॉल्ट हैं। और वे इसे बदल सकते हैं। आप अपना स्थान प्राप्त करने के लिए NSLocale की जांच कर सकते हैं। –

0

यह आज पाया एक ही सवाल पूछने के साथ जा रहा ....:,

 
NSString *rvalue; 
    if (value > 1000) { 
     rvalue = [NSString stringWithFormat:@"%.02f km",value]; 
    }else { 
     rvalue = [NSString stringWithFormat:@"%.02f m",value]; 
    } 

एक विधि में इस लपेट सकता है अगर जरूरत हो

23

iOS 7 और ओएस एक्स 10.9 पेश किया MKDistanceFormatter स्वरूपण दूरी:

कोड उदाहरण:

double myDistance = 21837.0f; 

MKDistanceFormatter *df = [[MKDistanceFormatter alloc]init]; 
df.unitStyle = MKDistanceFormatterUnitStyleAbbreviated; 

NSLog(@"myDistance is %@", [df stringFromDistance: myDistance]); 

अद्यतन:

ऐसा नहीं है कि MKDistanceFormatter इनपुट मूल्य किसी भी तरह गोलाई है लगता है। जैसे जब मैंने अपनाडिस्टेंस 111.0 पर सेट किया तो मुझे "100 मीटर" मिलता है।

4

NSLengthFormatter जो आईओएस 8 और ओएस एक्स 10.10 के साथ पेश किया गया था, एक विकल्प लोगों को अवगत होना चाहिए।

NSLengthFormatter *lengthFormatter = [[NSLengthFormatter alloc] init]; 
    lengthFormatter.unitStyle = NSFormattingUnitStyleShort; 

    NSLog(@"distance = %@", [lengthFormatter stringFromMeters:meters]); 

हालांकि, NSLengthFormatter उन स्थानों पर जहां वे दूरी के लिए छोड़कर मीट्रिक का उपयोग में इंपीरियल इकाइयों का उपयोग नहीं करता है।

+0

यह अच्छा होगा अगर यह _yards_ के बजाय _feet_ लौटा। – inorganik

-1

उन लोगों के लिए जो एक तेज 2.0 संस्करण की तलाश में हैं। @MattDiPasquale

extension Double { 
    func toDistanceString() -> String { 
    let METERS_TO_FEET = 3.2808399 
    let METERS_CUTOFF = 1000.0 
    let FEET_CUTOFF = 3281.0 
    let FEET_IN_MILES = 5280.0 

    let format:String 
    if (NSLocale.isMetric()) { 
     if (self < METERS_CUTOFF) { 
     format = "\(self.stringWithDecimals(0)) metres" 
     } else { 
     format = "\((self/1000).stringWithDecimals(1)) km"; 
     } 
    } else { // assume Imperial/U.S. 
     let feet = self * METERS_TO_FEET; 
     if (feet < FEET_CUTOFF) { 
     format = "\(feet.stringWithDecimals(0)) feet"; 
     } else { 
     format = "\((self/FEET_IN_MILES).stringWithDecimals(1)) miles"; 
     } 
    } 
    return format 
    } 

    func stringWithDecimals(decimals:Int) -> String { 
    return String(format: "%.\(decimals)f", self) 
    } 
} 

extension NSLocale { 
    class func isMetric() -> Bool { 
    let locale = NSLocale.currentLocale() 
    return locale.objectForKey(NSLocaleUsesMetricSystem) as! Bool 
    } 
} 
संबंधित मुद्दे