2009-02-13 12 views
12

में एनएसएसटींग को अपनी मुद्रा में बराबर करने का सबसे आसान तरीका है मेरे पास @ 7878 "का एनएसएसटींग मान है। मैं इसे मुद्रा प्रारूप में कैसे प्राप्त करूं, यानी $ 78,000 के साथ यह एक एनएसएसटींग शेष है।कोको

उत्तर

21

आपको एक संख्या फॉर्मेटर का उपयोग करने की आवश्यकता है। इस प्रकार ध्यान दें कि यह भी है कि कैसे आप उपयोगकर्ताओं को स्थान के लिए

// alloc formatter 
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init]; 

// set options. 
[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle]; 

NSNumber *amount = [NSNumber numberWithInteger:78000]; 

// get formatted string 
NSString* formatted = [currencyStyle stringFromNumber:amount] 

[currencyStyle release]; 
3

दशमलव सही स्वरूप में दिनांक/बार आदि को प्रदर्शित होगी/अंत में सेंट नियंत्रित किया जा सकता:

[currencyStyle setMaximumFractionDigits:0]; 

यहाँ प्रलेखन है लिंक apple

3

से जवाब ऊपर यहाँ एक नंबर में परिवर्तित कर देंगे, लेकिन वास्तव में मुद्रा प्रारूप करने के लिए एक NSString परिवर्तित करने के लिए एक विधि है

- (NSString*) formatCurrencyWithString: (NSString *) string 
{ 
    // alloc formatter 
    NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init]; 

    // set options. 
    [currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4]; 

    // reset style to no style for converting string to number. 
    [currencyStyle setNumberStyle:NSNumberFormatterNoStyle]; 

    //create number from string 
    NSNumber * balance = [currencyStyle numberFromString:string]; 

    //now set to currency format 
    [currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle]; 

    // get formatted string 
    NSString* formatted = [currencyStyle stringFromNumber:balance]; 

    //release 
    [currencyStyle release]; 

    //return formatted string 
    return formatted; 
}