Форматирование вывода с настройками валюты устройства - PullRequest
0 голосов
/ 06 июня 2011

Я работаю над своим первым приложением для iPhone и хотел бы иметь возможность форматировать валюту с помощью настроек валюты устройства.

В настоящее время у меня есть это:

- (IBAction)calculate {

    float lp = ([leftPrice.text floatValue]);
    float rp = ([rightPrice.text floatValue]);
    float lw = ([leftWeight.text floatValue]);
    float rw = ([rightWeight.text floatValue]);

    float lppkg = lp/lw;
    float rppkg = rp/rw;

    if (lp > 0 && lw > 0) {
        leftPricePerKg.text = [[NSString alloc] initWithFormat:@"$%.2f", lppkg];
    }

    if (rp > 0 && rw > 0) {
        rightPricePerKg.text = [[NSString alloc] initWithFormat:@"$%.2f", rppkg];
    }
}

1 Ответ

2 голосов
/ 06 июня 2011

Вы должны проверить NSNumberFormatter -

Вы можете посмотреть здесь: https://developer.apple.com/documentation/foundation/numberformatter

А здесь:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfNumberFormatting10_4.html#//apple_ref/doc/uid/TP40002368-SW1

Особенно в

«Настройка формата валюты», часть

Редактировать

Я обнаружил, что -

//--------------------------------------------
// Format style as currency, output to console
//--------------------------------------------
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
formattedOutput = [formatter stringFromNumber:[attributesDict objectForKey:NSFileSystemFreeSize]];
NSLog(@"Output as currency: %@", formattedOutput);

//--------------------------------------------
// Change local and output as currency
//--------------------------------------------
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"it_IT"];
[formatter setLocale:locale];
formattedOutput = [formatter stringFromNumber:[attributesDict objectForKey:NSFileSystemFreeSize]];
NSLog(@"Output as currency - locale it_IT: %@", formattedOutput);

Здесь: http://iosdevelopertips.com/cocoa/formatting-numbers-nsnumberformatter-examples.html

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...